Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
mainwindow.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3#include "mainwindow.h"
4
5#include <QMenu>
6#include <QMenuBar>
7#include <QTextEdit>
8#include <QFileDialog>
9#include <QPrinter>
10#include <QPrintDialog>
11
13{
14 QMenu *fileMenu = new QMenu(tr("&File"));
15
16 fileMenu->addAction(tr("&Open..."), QKeySequence(tr("Ctrl+O", "File|Open"))
17 this, SLOT(openFile()));
18
19 printAction = fileMenu->addAction(tr("&Print..."), this, SLOT(printFile()));
20 printAction->setEnabled(false);
21
22 pdfPrintAction = fileMenu->addAction(tr("Print as P&DF..."), this, SLOT(printPdf()));
23 pdfPrintAction->setEnabled(false);
24
25 fileMenu->addAction(tr("E&xit"), QKeySequence(tr("Ctrl+Q", "File|Exit")),
26 this, SLOT(close()));
27
28 menuBar()->addMenu(fileMenu);
29
30 editor = new QTextEdit(this);
31 document = new QTextDocument(this);
32 editor->setDocument(document);
33
35
36 setCentralWidget(editor);
37 setWindowTitle(tr("Text Document Writer"));
38}
39
41{
43 tr("Open file"), currentFile, "HTML files (*.html);;Text files (*.txt)");
44
45 if (!fileName.isEmpty()) {
47 if (info.completeSuffix() == "html") {
49
51 editor->setHtml(file.readAll());
52 file.close();
53 currentFile = fileName;
54 }
55 } else if (info.completeSuffix() == "txt") {
57
59 editor->setPlainText(file.readAll());
60 file.close();
61 currentFile = fileName;
62 }
63 }
64 printAction->setEnabled(true);
65 pdfPrintAction->setEnabled(true);
66 }
67}
68
70{
72 QTextDocument *document = editor->document();
73 QPrinter printer;
74
75 QPrintDialog *dlg = new QPrintDialog(&printer, this);
76 if (dlg->exec() != QDialog::Accepted)
77 return;
78
79 document->print(&printer);
81}
82
84{
87
88 QPrintDialog *printDialog = new QPrintDialog(&printer, this);
90 editor->document()->print(&printer);
91}
void openFile()
void printFile()
void printPdf()
void updateMenus()
void setEnabled(bool)
Definition qaction.cpp:927
@ Accepted
Definition qdialog.h:30
void close() override
Calls QFileDevice::flush() and closes the file.
static QString getOpenFileName(QWidget *parent=nullptr, const QString &caption=QString(), const QString &dir=QString(), const QString &filter=QString(), QString *selectedFilter=nullptr, Options options=Options())
This is a convenience static function that returns an existing file selected by the user.
\inmodule QtCore
Definition qfile.h:93
QFILE_MAYBE_NODISCARD bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:904
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
The QKeySequence class encapsulates a key sequence as used by shortcuts.
void setCentralWidget(QWidget *widget)
Sets the given widget to be the main window's central widget.
QAction * addMenu(QMenu *menu)
Appends menu to the menu bar.
Definition qmenubar.cpp:768
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition qmenu.h:26
void addAction(QAction *action)
Appends the action action to this widget's list of actions.
Definition qwidget.cpp:3117
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
The QPrintDialog class provides a dialog for specifying the printer's configuration.
int exec() override
\reimp
\reentrant
Definition qprinter.h:28
@ PdfFormat
Definition qprinter.h:69
void setOutputFormat(OutputFormat format)
Definition qprinter.cpp:549
@ HighResolution
Definition qprinter.h:31
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\reentrant \inmodule QtGui
void print(QPagedPaintDevice *printer) const
Prints the document to the given printer.
The QTextEdit class provides a widget that is used to edit and display both plain and rich text.
Definition qtextedit.h:27
void setPlainText(const QString &text)
Changes the text of the text edit to the string text.
void setHtml(const QString &text)
void setDocument(QTextDocument *document)
QTextDocument * document
the underlying document of the text editor.
Definition qtextedit.h:51
void selectionChanged()
This signal is emitted whenever the selection changes.
bool close()
Closes this widget.
Definition qwidget.cpp:8562
void setWindowTitle(const QString &)
Definition qwidget.cpp:6105
#define SLOT(a)
Definition qobjectdefs.h:52
#define tr(X)
QFile file
[0]
QPrintDialog printDialog(printer, parent)
[0]
QMenuBar * menuBar
[0]
QHostInfo info
[0]