diff options
Diffstat (limited to 'doc/src/snippets/widgets-tutorial')
21 files changed, 328 insertions, 68 deletions
diff --git a/doc/src/snippets/widgets-tutorial/notepad/copy.png b/doc/src/snippets/widgets-tutorial/notepad/copy.png Binary files differnew file mode 100644 index 000000000..cb3442c04 --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/copy.png diff --git a/doc/src/snippets/widgets-tutorial/notepad/create.png b/doc/src/snippets/widgets-tutorial/notepad/create.png Binary files differnew file mode 100644 index 000000000..fdfd4b438 --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/create.png diff --git a/doc/src/snippets/widgets-tutorial/notepad/cut.png b/doc/src/snippets/widgets-tutorial/notepad/cut.png Binary files differnew file mode 100644 index 000000000..74b15301f --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/cut.png diff --git a/doc/src/snippets/widgets-tutorial/notepad/edit_redo.png b/doc/src/snippets/widgets-tutorial/notepad/edit_redo.png Binary files differnew file mode 100644 index 000000000..8a7725463 --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/edit_redo.png diff --git a/doc/src/snippets/widgets-tutorial/notepad/edit_undo.png b/doc/src/snippets/widgets-tutorial/notepad/edit_undo.png Binary files differnew file mode 100644 index 000000000..852f5e3b2 --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/edit_undo.png diff --git a/doc/src/snippets/widgets-tutorial/notepad/exit.png b/doc/src/snippets/widgets-tutorial/notepad/exit.png Binary files differnew file mode 100644 index 000000000..677d4deef --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/exit.png diff --git a/doc/src/snippets/widgets-tutorial/notepad/font.png b/doc/src/snippets/widgets-tutorial/notepad/font.png Binary files differnew file mode 100644 index 000000000..925e501c0 --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/font.png diff --git a/doc/src/snippets/widgets-tutorial/notepad/info.png b/doc/src/snippets/widgets-tutorial/notepad/info.png Binary files differnew file mode 100644 index 000000000..9731212c4 --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/info.png diff --git a/doc/src/snippets/widgets-tutorial/notepad/main.cpp b/doc/src/snippets/widgets-tutorial/notepad/main.cpp index 81c8c20d3..5a76f0255 100644 --- a/doc/src/snippets/widgets-tutorial/notepad/main.cpp +++ b/doc/src/snippets/widgets-tutorial/notepad/main.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the documentation of the Qt Toolkit. @@ -59,17 +59,17 @@ int main(int argc, char *argv[]) { //! [2] //! [3] - QApplication a(argc, argv); + QApplication EditorApp(argc, argv); //! [3] //! [4] - Notepad w; + Notepad Editor; //! [4] //! [5] - w.show(); + Editor.show(); //! [5] //! [6] - return a.exec(); + return EditorApp.exec(); //! [6] } //! [all] diff --git a/doc/src/snippets/widgets-tutorial/notepad/new.png b/doc/src/snippets/widgets-tutorial/notepad/new.png Binary files differnew file mode 100644 index 000000000..b24edc5d0 --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/new.png diff --git a/doc/src/snippets/widgets-tutorial/notepad/notepad.cpp b/doc/src/snippets/widgets-tutorial/notepad/notepad.cpp index c7ef71bcf..f519bb66a 100644 --- a/doc/src/snippets/widgets-tutorial/notepad/notepad.cpp +++ b/doc/src/snippets/widgets-tutorial/notepad/notepad.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the documentation of the Qt Toolkit. @@ -52,66 +52,139 @@ #include "ui_notepad.h" //! [0] -#include <QFileDialog> #include <QFile> -#include <QMessageBox> +#include <QFileDialog> #include <QTextStream> +#include <QMessageBox> +#include <QprintDev> +#include <QPrintDialog> +#include <QFont> +#include <QFontDialog> //! [0] +//! [1] Notepad::Notepad(QWidget *parent) : QMainWindow(parent), ui(new Ui::Notepad) { ui->setupUi(this); + this->setCentralWidget(ui->textEdit); } +//! [1] Notepad::~Notepad() { delete ui; } -//! [1] -void Notepad::on_quitButton_clicked() +//! [2] +void Notepad::on_actionNew_triggered() { - QCoreApplication::quit(); + currentFile = ""; + ui->textEdit->setText(""); } -//! [1] - //! [2] + +//! [3] void Notepad::on_actionOpen_triggered() { - QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(), - tr("Text Files (*.txt);;C++ Files (*.cpp *.h)")); - - if (!fileName.isEmpty()) { - QFile file(fileName); - if (!file.open(QIODevice::ReadOnly)) { - QMessageBox::critical(this, tr("Error"), tr("Could not open file")); - return; - } - QTextStream in(&file); - ui->textEdit->setText(in.readAll()); - file.close(); + QString fileName = QFileDialog::getOpenFileName(this, "Open the file"); + QFile file(fileName); + currentFile = fileName; + if (!file.open(QIODevice::ReadOnly | QFile::Text)) { + QMessageBox::warning(this,"..","File not opened."); + return; } + QTextStream in(&file); + QString text = in.readAll(); + ui->textEdit->setText(text); + file.close(); } -//! [2] - //! [3] + +//! [4] void Notepad::on_actionSave_triggered() { - QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QString(), - tr("Text Files (*.txt);;C++ Files (*.cpp *.h)")); - - if (!fileName.isEmpty()) { - QFile file(fileName); - if (!file.open(QIODevice::WriteOnly)) { - // error message - } else { - QTextStream stream(&file); - stream << ui->textEdit->toPlainText(); - stream.flush(); - file.close(); + QFile file(currentFile); + if (!file.open(QIODevice::WriteOnly | QFile::Text)) { + QMessageBox::warning(this,"..","No file opened. Use Save As"); + return; + } + QTextStream out(&file); + QString text = ui->textEdit->toPlainText(); + out << text; + file.flush(); + file.close(); +} +//! [4] + +//! [5] +void Notepad::on_actionSave_as_triggered() +{ + QString fileName = QFileDialog::getSaveFileName(this, "Save as"); + QFile file(fileName); + currentFile = fileName; + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::warning(this,"..","File not opened."); + return; + } + QTextStream out(&file); + QString text = ui->textEdit->toPlainText(); + out << text; + file.flush(); + file.close(); +} +//! [5] + +//! [6] +void Notepad::on_actionPrint_triggered() +{ + QprintDev printDev; + QPrintDialog dialog(&printDev, this); + if (dialog.exec() == QDialog::Rejected) { + return; } + ui->textEdit->print(&printDev); +} +//! [6] + +void Notepad::on_actionExit_triggered() +{ + QCoreApplication::quit(); +} + +void Notepad::on_actionCopy_triggered() +{ + ui->textEdit->copy(); +} + +void Notepad::on_actionCut_triggered() +{ + ui->textEdit->cut(); +} + +void Notepad::on_actionPaste_triggered() +{ + ui->textEdit->paste(); +} + +void Notepad::on_actionUndo_triggered() +{ + ui->textEdit->undo(); +} + +void Notepad::on_actionRedo_triggered() +{ + ui->textEdit->redo(); +} + +//! [7] +void Notepad::on_actionFont_triggered() +{ + bool fontSelected; + QFont font = QFontDialog::getFont(&fontSelected, this); + if (fontSelected) { + ui->textEdit->setFont(font); } } -//! [3] +//! [7] diff --git a/doc/src/snippets/widgets-tutorial/notepad/notepad.h b/doc/src/snippets/widgets-tutorial/notepad/notepad.h index ea8961afe..9bcdd725a 100644 --- a/doc/src/snippets/widgets-tutorial/notepad/notepad.h +++ b/doc/src/snippets/widgets-tutorial/notepad/notepad.h @@ -51,29 +51,62 @@ #ifndef NOTEPAD_H #define NOTEPAD_H +//! [all] +//! [1] #include <QMainWindow> +//! [1] +//! [2] namespace Ui { class Notepad; } +//! [2] +//! [3] class Notepad : public QMainWindow { Q_OBJECT +//! [3] +//! [4] public: explicit Notepad(QWidget *parent = 0); +//! [4] +//! [5] ~Notepad(); +//! [5] private slots: - void on_quitButton_clicked(); + void on_actionNew_triggered(); void on_actionOpen_triggered(); void on_actionSave_triggered(); + void on_actionSave_as_triggered(); + + void on_actionPrint_triggered(); + + void on_actionExit_triggered(); + + void on_actionCopy_triggered(); + + void on_actionCut_triggered(); + + void on_actionPaste_triggered(); + + void on_actionUndo_triggered(); + + void on_actionRedo_triggered(); + + void on_actionFont_triggered(); + +//! [6] private: Ui::Notepad *ui; + QString CurrentFile; +//! [6] }; +//! [all] #endif // NOTEPAD_H diff --git a/doc/src/snippets/widgets-tutorial/notepad/notepad.pro b/doc/src/snippets/widgets-tutorial/notepad/notepad.pro index 1aadcd306..758c067eb 100644 --- a/doc/src/snippets/widgets-tutorial/notepad/notepad.pro +++ b/doc/src/snippets/widgets-tutorial/notepad/notepad.pro @@ -1,10 +1,27 @@ -QT += core gui +#------------------------------------------------- +# +# Project created by QtCreator 2017-07-10T14:20:07 +# +#------------------------------------------------- + +QT += core gui printsupport greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = Notepad TEMPLATE = app +# The following define makes your compiler emit warnings if you use +# any feature of Qt which as been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + SOURCES += main.cpp\ notepad.cpp @@ -12,3 +29,6 @@ SOURCES += main.cpp\ HEADERS += notepad.h FORMS += notepad.ui + +RESOURCES += \ + notepad.qrc diff --git a/doc/src/snippets/widgets-tutorial/notepad/notepad.qrc b/doc/src/snippets/widgets-tutorial/notepad/notepad.qrc new file mode 100644 index 000000000..3e04ad7e7 --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/notepad.qrc @@ -0,0 +1,19 @@ +<RCC> + <qresource prefix="/"> + <file>info.png</file> + <file>new.png</file> + <file>open.png</file> + <file>paste.png</file> + <file>pencil.png</file> + <file>print.png</file> + <file>save.png</file> + <file>save_as.png</file> + <file>exit.png</file> + <file>font.png</file> + <file>copy.png</file> + <file>create.png</file> + <file>cut.png</file> + <file>edit_redo.png</file> + <file>edit_undo.png</file> + </qresource> +</RCC> diff --git a/doc/src/snippets/widgets-tutorial/notepad/notepad.ui b/doc/src/snippets/widgets-tutorial/notepad/notepad.ui index 950cf718d..59dfce383 100644 --- a/doc/src/snippets/widgets-tutorial/notepad/notepad.ui +++ b/doc/src/snippets/widgets-tutorial/notepad/notepad.ui @@ -6,7 +6,7 @@ <rect> <x>0</x> <y>0</y> - <width>400</width> + <width>524</width> <height>300</height> </rect> </property> @@ -14,46 +14,47 @@ <string>Notepad</string> </property> <widget class="QWidget" name="centralWidget"> - <widget class="QWidget" name=""> - <property name="geometry"> - <rect> - <x>70</x> - <y>0</y> - <width>268</width> - <height>235</height> - </rect> - </property> - <layout class="QVBoxLayout" name="verticalLayout"> - <item> - <widget class="QTextEdit" name="textEdit"/> - </item> - <item> - <widget class="QPushButton" name="quitButton"> - <property name="text"> - <string>Quit</string> - </property> - </widget> - </item> - </layout> - </widget> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QTextEdit" name="textEdit"/> + </item> + </layout> </widget> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> - <width>400</width> - <height>22</height> + <width>524</width> + <height>25</height> </rect> </property> <widget class="QMenu" name="menuFile"> <property name="title"> <string>File</string> </property> + <addaction name="actionNew"/> <addaction name="actionOpen"/> <addaction name="actionSave"/> + <addaction name="actionSave_as"/> + <addaction name="actionPrint"/> + <addaction name="separator"/> + <addaction name="actionExit"/> + </widget> + <widget class="QMenu" name="menuEdit"> + <property name="title"> + <string>Edit</string> + </property> + <addaction name="actionCopy"/> + <addaction name="actionCut"/> + <addaction name="actionPaste"/> + <addaction name="separator"/> + <addaction name="actionUndo"/> + <addaction name="actionRedo"/> + <addaction name="actionFont"/> </widget> <addaction name="menuFile"/> + <addaction name="menuEdit"/> </widget> <widget class="QToolBar" name="mainToolBar"> <attribute name="toolBarArea"> @@ -62,20 +63,134 @@ <attribute name="toolBarBreak"> <bool>false</bool> </attribute> + <addaction name="actionNew"/> + <addaction name="actionOpen"/> + <addaction name="actionSave"/> + <addaction name="actionSave_as"/> + <addaction name="actionPrint"/> + <addaction name="separator"/> + <addaction name="actionCopy"/> + <addaction name="actionCut"/> + <addaction name="actionPaste"/> + <addaction name="actionUndo"/> + <addaction name="actionRedo"/> + <addaction name="actionFont"/> + <addaction name="separator"/> + <addaction name="actionExit"/> </widget> <widget class="QStatusBar" name="statusBar"/> + <action name="actionNew"> + <property name="icon"> + <iconset resource="notepad.qrc"> + <normaloff>:/new.png</normaloff>:/new.png</iconset> + </property> + <property name="text"> + <string>New</string> + </property> + </action> <action name="actionOpen"> + <property name="icon"> + <iconset resource="notepad.qrc"> + <normaloff>:/open.png</normaloff>:/open.png</iconset> + </property> <property name="text"> <string>Open</string> </property> </action> <action name="actionSave"> + <property name="icon"> + <iconset resource="notepad.qrc"> + <normaloff>:/save.png</normaloff>:/save.png</iconset> + </property> <property name="text"> <string>Save</string> </property> </action> + <action name="actionSave_as"> + <property name="icon"> + <iconset resource="notepad.qrc"> + <normaloff>:/save_as.png</normaloff>:/save_as.png</iconset> + </property> + <property name="text"> + <string>Save as</string> + </property> + </action> + <action name="actionPrint"> + <property name="icon"> + <iconset resource="notepad.qrc"> + <normaloff>:/print.png</normaloff>:/print.png</iconset> + </property> + <property name="text"> + <string>Print</string> + </property> + </action> + <action name="actionExit"> + <property name="icon"> + <iconset resource="notepad.qrc"> + <normaloff>:/exit.png</normaloff>:/exit.png</iconset> + </property> + <property name="text"> + <string>Exit</string> + </property> + </action> + <action name="actionCopy"> + <property name="icon"> + <iconset resource="notepad.qrc"> + <normaloff>:/copy.png</normaloff>:/copy.png</iconset> + </property> + <property name="text"> + <string>Copy</string> + </property> + </action> + <action name="actionCut"> + <property name="icon"> + <iconset resource="notepad.qrc"> + <normaloff>:/cut.png</normaloff>:/cut.png</iconset> + </property> + <property name="text"> + <string>Cut</string> + </property> + </action> + <action name="actionPaste"> + <property name="icon"> + <iconset resource="notepad.qrc"> + <normaloff>:/paste.png</normaloff>:/paste.png</iconset> + </property> + <property name="text"> + <string>Paste</string> + </property> + </action> + <action name="actionUndo"> + <property name="icon"> + <iconset resource="notepad.qrc"> + <normaloff>:/edit_undo.png</normaloff>:/edit_undo.png</iconset> + </property> + <property name="text"> + <string>Undo</string> + </property> + </action> + <action name="actionRedo"> + <property name="icon"> + <iconset resource="notepad.qrc"> + <normaloff>:/edit_redo.png</normaloff>:/edit_redo.png</iconset> + </property> + <property name="text"> + <string>Redo</string> + </property> + </action> + <action name="actionFont"> + <property name="icon"> + <iconset resource="notepad.qrc"> + <normaloff>:/font.png</normaloff>:/font.png</iconset> + </property> + <property name="text"> + <string>Font</string> + </property> + </action> </widget> <layoutdefault spacing="6" margin="11"/> - <resources/> + <resources> + <include location="notepad.qrc"/> + </resources> <connections/> </ui> diff --git a/doc/src/snippets/widgets-tutorial/notepad/open.png b/doc/src/snippets/widgets-tutorial/notepad/open.png Binary files differnew file mode 100644 index 000000000..7b052edf5 --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/open.png diff --git a/doc/src/snippets/widgets-tutorial/notepad/paste.png b/doc/src/snippets/widgets-tutorial/notepad/paste.png Binary files differnew file mode 100644 index 000000000..c50dbd95b --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/paste.png diff --git a/doc/src/snippets/widgets-tutorial/notepad/pencil.png b/doc/src/snippets/widgets-tutorial/notepad/pencil.png Binary files differnew file mode 100644 index 000000000..a9c5e5482 --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/pencil.png diff --git a/doc/src/snippets/widgets-tutorial/notepad/print.png b/doc/src/snippets/widgets-tutorial/notepad/print.png Binary files differnew file mode 100644 index 000000000..0cd3f28bd --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/print.png diff --git a/doc/src/snippets/widgets-tutorial/notepad/save.png b/doc/src/snippets/widgets-tutorial/notepad/save.png Binary files differnew file mode 100644 index 000000000..e65a29d5f --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/save.png diff --git a/doc/src/snippets/widgets-tutorial/notepad/save_as.png b/doc/src/snippets/widgets-tutorial/notepad/save_as.png Binary files differnew file mode 100644 index 000000000..604057432 --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/save_as.png |