diff options
author | Nico Vertriest <nico.vertriest@qt.io> | 2017-07-11 15:49:03 +0200 |
---|---|---|
committer | Nico Vertriest <nico.vertriest@qt.io> | 2017-08-30 10:00:03 +0000 |
commit | 7ec16b36a793138f1d1891e96156a289af30149a (patch) | |
tree | ec3715409fb0a6cafea9d0745afb62458b6281f8 /doc | |
parent | 48f7e029923303fc313ca460a1a7d06fc2d04384 (diff) | |
download | qtdoc-7ec16b36a793138f1d1891e96156a289af30149a.tar.gz |
Doc: new tutorial qt widgets Notepad example
- new example having options open, save, save as, print.
- updated tutorial
Change-Id: I1dd716904cd25695b568b23c1ba283bc0dc588a5
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'doc')
28 files changed, 440 insertions, 272 deletions
diff --git a/doc/src/getting-started/gettingstartedqt.qdoc b/doc/src/getting-started/gettingstartedqt.qdoc index 73e3a2a2..7c448c28 100644 --- a/doc/src/getting-started/gettingstartedqt.qdoc +++ b/doc/src/getting-started/gettingstartedqt.qdoc @@ -31,22 +31,11 @@ \title Getting Started Programming with Qt Widgets In this topic, we teach basic Qt knowledge by implementing a simple Notepad - application using C++ and the \l{Qt Widgets} module. We use the - Qt Creator IDE and Qt Designer to generate some code, but you could also write all the - code yourself. + application using C++ and the \l{Qt Widgets} module. The application is a + small text editor which allows you to create a text file, save it, print it, + or reopen and edit it again. You can also set the font to be used. - After reading this topic, you are ready to refer to our overviews and API - documentation, to find the information you need for the application you are - developing. - - In this topic, we first use Qt Creator to create a project with the - necessary files. Then we use Qt Designer to modify the user interface files - to show a text edit and a push button in a window on the desktop. - This represents a simple Qt application that has a GUI. Finally, we add - user interaction to the application by creating actions for opening and - saving files. - - \image gs1.png "Notepad application" + \image notepad1.png "Notepad application" You can find the final Notepad source files in the qtdoc repository in the snippets/widgets-tutorial/notepad directory. You can either fetch @@ -59,14 +48,14 @@ to enter the settings needed for that particular type of project and creates the project for you. - \image gs-project1.png "Qt Creator New File or Project dialog" + \image notepad2.png "Qt Creator New File or Project dialog" To create the Notepad project, select \b File > \b{New File or Project} > \b Applications > \b {Qt Widgets Application} > \b Choose, and follow the instructions of the wizard. In the \b {Class Information} dialog, type \b Notepad as the class name and select \b QMainWindow as the base class. - \image gs-project2.png "Class Information Dialog" + \image notepad3.png "Class Information Dialog" The \b {Qt Widgets Application} wizard creates a project that contains a main source file and a set of files that specify a user interface (Notepad @@ -107,7 +96,7 @@ \snippet snippets/widgets-tutorial/notepad/main.cpp all - Let us go through the code line by line. The following lines include + We will go through the code line by line. The following lines include the header files for the Notepad widget and QApplication. All Qt classes have a header file named after them. @@ -120,9 +109,9 @@ The following line creates a QApplication object. This object manages application-wide resources and is necessary to run any Qt program - that uses Qt Widgets. It constructs an application object with \c argc command - line arguments run in \c argv. (For GUI applications that do not use Qt Widgets, you can use - QGuiApplication instead.) + that uses Qt Widgets. It constructs an application object with \c argc + command line arguments run in \c argv. (For GUI applications that do not + use Qt Widgets, you can use QGuiApplication instead.) \snippet snippets/widgets-tutorial/notepad/main.cpp 3 @@ -137,12 +126,10 @@ \snippet snippets/widgets-tutorial/notepad/main.cpp 4 The following line shows the Notepad widget on the screen in its own - window. - Since widgets also function as containers (for instance a - QMainWindow, which has toolbars, menus, a status bar, and a few - other widgets), it is possible to show a single widget in its own - window. Widgets are not visible by default; the function - \l{QWidget::}{show()} makes the widget visible. + window. Widgets can also function as containers. An example of this + is QMainWindow which often contains several types of widgets. Widgets + are not visible by default; the function \l{QWidget::}{show()} makes + the widget visible. \snippet snippets/widgets-tutorial/notepad/main.cpp 5 @@ -169,7 +156,7 @@ \section1 Designing a UI - The wizard generates a user interface definition in XML format, notepad.ui. + The wizard generates a user interface definition in XML format: notepad.ui. When you open the notepad.ui file in Qt Creator, it automatically opens in the integrated Qt Designer. @@ -180,29 +167,17 @@ \section2 Using Qt Designer The wizard creates an application that uses a QMainWindow. It has its own - layout to which you can add a menu bar, dock widgets, tool bars, and a + layout to which you can add a menu bar, dock widgets, toolbars, and a status bar. The center area can be occupied by any kind of widget. The wizard places the Notepad widget there. - Let us use Qt Designer to add a QTextEdit object and a QPushButton object to - the main window. When you type text in the text edit widget, it receives key - pressed events and responds by drawing the text typed. The button will exit - the Notepad application when pushed (that is, clicked with the mouse). - To add widgets in Qt Designer: \list 1 \li In the Qt Creator \b Editor mode, double-click the notepad.ui file in the \b Projects view to launch the file in the integrated Qt Designer. - \li Drag and drop the following widgets to the form: - \list - \li Text Edit (QTextEdit) - \li Push Button (QPushButton) - \endlist - \li Double-click the \b {Push Button} widget and enter the text \b Quit. - \li In the \b Properties pane, change the value of \b objectName to - \b quitButton. + \li Drag and drop widgets Text Edit (QTextEdit) to the form. \li Press \b {Ctrl+A} (or \b {Cmd+A}) to select the widgets and click \b {Lay out Vertically} (or press \b {Ctrl+L}) to apply a vertical layout (QVBoxLayout). @@ -211,7 +186,7 @@ The UI now looks as follows in Qt Designer: - \image gs2.png + \image notepad4.png You can view the generated XML file in the code editor: @@ -234,28 +209,6 @@ <ui version="4.0"> \endcode - The following snippet creates a QVBoxLayout widget that contains a QTextEdit - and QPushButton widget. As mentioned, widgets can contain - other widgets. It is possible to set the bounds (the location and - size) of child widgets directly, but it is usually easier to use a - layout. A layout manages the bounds of a widget's children. - QVBoxLayout, for instance, places the children in a vertical row. - - \code - <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> - \endcode - The UI file is used together with the header and source file of the Notepad class. We will look at the rest of the UI file in the later sections. @@ -282,7 +235,7 @@ definition, and declares our class as a QObject. Naturally, it must also inherit from QObject. A QObject adds several abilities to a normal C++ class. Notably, the class name and slot names can be - queried at run-time. It is also possible to query a slot's + queried at runtime. It is also possible to query a slot's parameter types and invoke it. \snippet snippets/gs/notepad1.h 3 @@ -343,6 +296,11 @@ \snippet snippets/gs/notepad1.cpp 5 + In order to have the text edit field occupy the whole screen, we add + \c setCentralWidget to the main window. + + \snippet snippets/widgets-tutorial/notepad/notepad.cpp 1 + \section2 Project File The wizard generates the following project file, \c {notepad.pro}, for us: @@ -389,179 +347,129 @@ \section1 Adding User Interaction - We now have a user interface, but it does not really do anything useful, as - it only contains a text edit and a push button, as well as some standard - functions for quitting, minimizing and maximizing the application. To make - the application useful, we will add user interaction to it. First, we will - add functionality to the push button. Second, we will add functions for - loading a file to the text edit and for saving the contents of the text edit - as a file. - - \section2 Adding Push Buttons - - Most desktop operating systems have standard ways of enabling users to quit - applications. However, in this example we use this basic function to - illustrate how you can add user interaction to applications. To do this, we - add a slot that we connect to the \b {Quit button}. - - To exit the application when the \b Quit button is pushed, you use the Qt - signals and slots mechanism. A signal is emitted when a particular event - occurs and a slot is a function that is called in response to a particular - signal. Qt widgets have predefined signals and slots that you can use - directly from Qt Designer. - - To use Qt Designer to add a slot for the quit function, right-click the - \b Quit button to open a context-menu and then select \b {Go to slot} > - \b {clicked()}. - - A private slot, \c{on_quitButton_clicked()}, is added to the Notepad widget - class header file, notepad.h and a private function, - \c{Notepad::on_quitButton_clicked()}, is added to the Notepad widget class - source file, notepad.cpp. We just need to write the code to execute the quit - function in the source file. - - Let us look at the modified code in the header file, notepad.h: - - \snippet snippets/gs/notepad2.h all - - The following code uses Qt's signals and slots mechanism to make the - application exit when the \b {Quit button} is pushed. Qt Designer uses - QMetaObject \l{designer-using-a-ui-file.html#automatic-connections} - {auto-connection facilities} to connect the button's clicked() signal to a - slot in the Notepad class. The \c uic tool automatically generates code in - the dialog's \c{setupUi()} function to do this, so Qt Designer only needs to - declare and implement a slot with a name that follows a standard convention. - - \snippet snippets/gs/notepad2.h 1 - The corresponding code in the source file, notepad.cpp, looks as follows: + To add functionality to the editor, we start by adding menu items and buttons on a toolbar. - \snippet snippets/gs/notepad2.cpp all + Click on "Type Here", and add the options New, Open, Save, Save as, Print + and Exit. This creates 5 lines in the Action Editor below. To connect the + actions to slots, right-click an action and select Go to slot > triggered(), + and complete the code for that given slot. - The code defines the private function that is executed when QPushButton - emits the \l{QPushButton::}{clicked()} signal. + If we also want to add the actions to a toolbar, we can assign an icon + to each QAction, and then drag the QAction to the toolbar. You assign an icon + by entering an icon name in the Icon property of the action concerned. When the QAction + has been dragged to the toolbar, clicking the icon will launch the associated + slot. - We now complement the code to have the \l{QCoreApplication::}{quit()} slot of - QApplication exit Notepad: + Complete the method \c on_actionNew_triggered() : - \snippet snippets/widgets-tutorial/notepad/notepad.cpp 1 - - \b{Learn More} - - \table - \header - \li About - \li Here - \row - \li Signals and slots - \li \l{Signals & Slots} - \endtable + \snippet snippets/widgets-tutorial/notepad/notepad.cpp 2 - \section2 Adding Menu Items + \c current_file is a global variable containing the file presently being edited. + It is defined in the private part of notepad.h: - Often, in a main window, the same slot should be invoked by - several widgets. Examples are menu items and buttons on a tool - bar. To make this easier, Qt provides QAction, which can be given - to several widgets, and be connected to a slot. For instance, both - QMenu and QToolBar can create menu items and tool buttons from the - same \l{QAction}. + \snippet snippets/widgets-tutorial/notepad/notepad.h 6 - To learn how to use actions with signals and slots, we add menu items to - open and save a document and connect them to slots. + \c setText("") clears the text buffer. - As before, we use Qt Designer to add the widgets to the user interface. - The wizard creates an application with a QMenu widget, with the text - \b {Type Here} as a placeholder for menu and menu item names. Double-click - the text to enter names for the \b File menu and \b Open and \b Save menu - items. Qt Designer automatically generates the appropriate actions. + \section2 Opening a file - \image gs3.png + In \c notepad.ui, right click on \c actionOpen and select \c {Go to slot} - To connect the actions to slots, right-click an action and select - \b {Go to slot} > \b triggered(). + Complete method \c on_actionOpen_triggered(). - \l{QAction} instances are created with the text that should appear on the - widgets that we add them to (in our case, menu items). If we also - wanted to add the actions to a tool bar, we could have specified - \l{QIcon}{icons} for them. + \snippet snippets/widgets-tutorial/notepad/notepad.cpp 3 - The modified code in notepad.ui now looks as follows: + \c QFileDialog::getOpenFileName opens a dialog enabling you to select a file. + QFile object \c myfile has the selected \c file_name as parameter. + We store the selected file also into the global variable \c current_file for later purposes. + We open the file with \c file.open as a readonly text file. If it + cannot be opened, a warning is issued, and the program stops. - \quotefromfile snippets/widgets-tutorial/notepad/notepad.ui + We define a QTextStream \c instream for parameter \c myfile. + The contents of file \c myfile is copied into QString \a text. + \c setText(text) fille the buffer of our editor with \c text. - \skipto QMenuBar - \printto layoutdefault + \c section2 Saving a file - Qt Designer adds the private slots \c{on_actionOpen_triggered()} and - \c{on_actionSave_triggered()} to notepad.h and the private functions - \c{Notepad::on_actionOpen_triggered()} and - \c{Notepad::on_actionSave_triggered()} to notepad.cpp. + We create the method for saving a file in the same way as for \l {Opening a file}, + by right clicking on \c actionSave, and selecting \c {Go to Slot}. - In the following sections, we complement the code to load and save files. - When a menu item is clicked, the item triggers the action, and the - respective slot is invoked. + \snippet snippets/widgets-tutorial/notepad/notepad.cpp 4 - \section2 Opening Files + QFile object \c myfile is linked to global variable \c current_file, the variable that + contains the file we were working with. + If we cannot open \c myfile, an error message is issued and the method stops. + We create a QTextStream \c outstream. The contents of the editor buffer is converted + to plain text, and then written to \c outstream. - In this section, we implement the functionality of the - \c{on_actionOpen_triggered()} slot. The first step is asking the user for - the name of the file to open. Qt comes with QFileDialog, which is a dialog - from which the user can select a file. The appearance of the dialog depends - on the desktop platform that you run the application on. The following - image shows the dialog on \macos: + \c section2 Saving a file with \c {Save as} - \image gs4.png + \snippet snippets/widgets-tutorial/notepad/notepad.cpp 5 - We complement the code generated by Qt Designer in notepad.cpp, as follows: + This is the same procedure as for \c {Saving a file}, the only difference + being that here you need to enter a new file name for the file to + be created. - \snippet snippets/widgets-tutorial/notepad/notepad.cpp 2 - The static \l{QFileDialog::}{getOpenFileName()} function - displays a modal file dialog. It returns the file path of the file - selected, or an empty string if the user canceled the dialog. + \section2 Print a file - If we have a file name, we try to open the file with - \l{QIODevice::}{open()}, which returns true if the file could be - opened. We will not go into error handling here, but you can follow - the links from the learn more section. If the file could not be - opened, we use QMessageBox to display a dialog with an error - message (see the QMessageBox class description for further - details). + If you want to use print functionalities, you need to add \c printsupport to + the project file: - Actually reading in the data is trivial using the QTextStream - class, which wraps the QFile object. The - \l{QTextStream::}{readAll()} function returns the contents of the - file as a QString. The contents can then be displayed in the text - edit. We then \l{QIODevice::}{close()} the file to return the file - descriptor back to the operating system. + QT += core gui printsupport - We now use the function \l{QObject::}{tr()} around our user - visible strings. This function is necessary when you want to - provide your application in more than one language (for example, English - and Chinese). We will not go into details here, but you can follow - the \c {Qt Linguist} link from the learn more table. + We declare a QPrinter object called \c printer. + We launch a printer dialog box and store the selected printer in object \c printer. + If we clicked on \c Cancel and did not select a printer, the methods returns. + The actual printer command is given with \a ui->textEdit->print with our QPrinter + object as parameter. - To use QFileDialog, QFile, QMessageBox, and QTextStream, add the following - includes to notepad.cpp: + \section2 Select a font - \snippet snippets/widgets-tutorial/notepad/notepad.cpp 0 + \snippet snippets/widgets-tutorial/notepad/notepad.cpp 7 - \section2 Saving Files + We declare a boolean indicating if we did select a font with QFontDialog. If so, we + set the font with \c ui->textEdit->setFont(myfont). - Now, let us move on to the \c{on_actionSave_triggered()} slot, which - also uses QFileDialog to create a dialog in which the user can save a - file with the specified name in the specified location. + \section2 Copy, cut, paste, undo, redo - \image gs5.png + If you select some text, and want to copy it to the clipboard, + you call the appropriate method of ui->textEdit. The same counts + for cut, paste, undo, and redo. - We complement the code generated by Qt Designer in notepad.cpp, as follows: + This table shows the method name to use. - \snippet snippets/widgets-tutorial/notepad/notepad.cpp 3 + \table + \header + \li Task + \li Method called + \row + \li Copy + \li ui->textEdit->copy() + \row + \li Cut + \li ui->textEdit->cut() + \row + \li Paste + \li ui->textEdit->paste() + \row + \li Undo + \li ui->textEdit->undo() + \row + \li Redo + \li ui->textEdit->redo() + \endtable - When we write the contents of the text edit to the file, we use - the QTextStream class again. QTextStream can also write - \l{QString}s to the file with the << operator. + This project contains the following files: + \list + \li {main.cpp} + \li {notepad.cpp} + \li {notepad.h} + \li {notepad.pro} + \li {notepad.ui} + \endlist \b{Learn More} \table diff --git a/doc/src/images/notepad.png b/doc/src/images/notepad.png Binary files differnew file mode 100644 index 00000000..40d13269 --- /dev/null +++ b/doc/src/images/notepad.png diff --git a/doc/src/images/notepad1.png b/doc/src/images/notepad1.png Binary files differnew file mode 100644 index 00000000..40d13269 --- /dev/null +++ b/doc/src/images/notepad1.png diff --git a/doc/src/images/notepad2.png b/doc/src/images/notepad2.png Binary files differnew file mode 100644 index 00000000..9cec1f9a --- /dev/null +++ b/doc/src/images/notepad2.png diff --git a/doc/src/images/notepad3.png b/doc/src/images/notepad3.png Binary files differnew file mode 100644 index 00000000..426861ab --- /dev/null +++ b/doc/src/images/notepad3.png diff --git a/doc/src/images/notepad4.png b/doc/src/images/notepad4.png Binary files differnew file mode 100644 index 00000000..fc08eab2 --- /dev/null +++ b/doc/src/images/notepad4.png diff --git a/doc/src/images/notepad_menu.png b/doc/src/images/notepad_menu.png Binary files differnew file mode 100644 index 00000000..2dd77111 --- /dev/null +++ b/doc/src/images/notepad_menu.png 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 00000000..cb3442c0 --- /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 00000000..fdfd4b43 --- /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 00000000..74b15301 --- /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 00000000..8a772546 --- /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 00000000..852f5e3b --- /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 00000000..677d4dee --- /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 00000000..925e501c --- /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 00000000..9731212c --- /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 81c8c20d..5a76f025 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 00000000..b24edc5d --- /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 c7ef71bc..f519bb66 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 ea8961af..9bcdd725 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 1aadcd30..758c067e 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 00000000..3e04ad7e --- /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 950cf718..59dfce38 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 00000000..7b052edf --- /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 00000000..c50dbd95 --- /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 00000000..a9c5e548 --- /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 00000000..0cd3f28b --- /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 00000000..e65a29d5 --- /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 00000000..60405743 --- /dev/null +++ b/doc/src/snippets/widgets-tutorial/notepad/save_as.png |