Create a QApplication object to manage application resources that are necessary for any uses Qt Widgets program. For there is no GUI application using Qt Widgets can be used QGuiApplication instead.
QApplication :: exec () into the event loop. Qt application is running, it will produce the event and sent to the widgets of the application. Event Example: mouse clicks and keyboard input.
Read More:
https://doc.qt.io/qt-5/application-windows.html
https://doc.qt.io/qt-5/eventsandfilters.html
When you open a file error occurs, a warning message:
if (!file.open(QIODevice::ReadOnly | QFile::Text)) { QMessageBox::warning(this, "Warning", "Cannot open file: " + file.errorString()); return; }
All characters get the file:
setWindowTitle(fileName); QTextStream in(&file); QString text = in.readAll();
Save all characters to the file
QFile file(fileName); if (!file.open(QIODevice::WriteOnly | QFile::Text)) { QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString()); return; } setWindowTitle(fileName); QTextStream out(&file); QString text = ui->textEdit->toPlainText(); out << text; file.close();
Printing Support:
qtHaveModule(printsupport): QT += printsupport
requires(qtConfig(fontdialog))
#if defined(QT_PRINTSUPPORT_LIB) #include <QtPrintSupport/qtprintsupportglobal.h> #if QT_CONFIG(printer) #if QT_CONFIG(printdialog) #include <QPrintDialog> #endif // QT_CONFIG(printdialog) #include <QPrinter> #endif // QT_CONFIG(printer) #endif // QT_PRINTSUPPORT_LIB ... void Notepad::print() { #if QT_CONFIG(printer) QPrinter printDev; #if QT_CONFIG(printdialog) QPrintDialog dialog(&printDev, this); if (dialog.exec() == QDialog::Rejected) return; #endif // QT_CONFIG(printdialog) ui->textEdit->print(&printDev); #endif // QT_CONFIG(printer) }
Copy, Cut, Paste
void Notepad::copy() { #if QT_CONFIG(clipboard) ui->textEdit->copy(); #endif } void Notepad::cut() { #if QT_CONFIG(clipboard) ui->textEdit->cut(); #endif } void Notepad::paste() { #if QT_CONFIG(clipboard) ui->textEdit->paste(); #endif }
Undo and Redo
void Notepad::undo() { ui->textEdit->undo(); } void Notepad::redo() { ui->textEdit->redo(); }
When you set font and italics / bold, etc., set in italics / bold, select the text can only take effect:
void Notepad::selectFont() { bool fontSelected; QFont font = QFontDialog::getFont(&fontSelected, this); if (fontSelected) ui->textEdit->setFont(font); } void Notepad::setFontUnderline(bool underline) { ui->textEdit->setFontUnderline(underline); } void Notepad::setFontItalic(bool italic) { ui->textEdit->setFontItalic(italic); } void Notepad::setFontBold(bool bold) { bold ? ui->textEdit->setFontWeight(QFont::Bold) : ui->textEdit->setFontWeight(QFont::Normal); }
About dialog:
void Notepad::about() { QMessageBox::about(this, tr("About MDI"), tr("The <b>Notepad</b> example demonstrates how to code a basic " "text editor using QtWidgets")); }