L1-L4 Lab.doc

(517 KB) Pobierz



Qt in Education

Lab 1 – The Basics of Qt

 

 

 

 

Aim:               This lab will take you through all the steps required to build a fully fledged Qt application. The focus is to understand how a Qt application is structured and to learn to find your way round the Qt documentation.

Duration:               3 h

 

1              Goals and Background

The goal of this lab is to apply the knowledge from the first lectures and put it in a context. The task at hand is to develop a fully fledged desktop application – a text editor with support for multiple documents, saving and loading, undoing, the clipboard, etc.



Throughout this lab, new features will be added to the application gradually. The purpose of this is twofold. First, this is how real world software is developed. The feature set grows over time. Secondly, you will have a working application from the very first step so that you can test, verify and debug the functionality continuously. Do not keep a known bug to a later step, make sure that the application works at all times. It will only be harder to properly troubleshoot in a more complex application.

In the lab, the instructions will be less detailed than in the exercises. This means that you are expected to look up classes in the Qt reference documentation, and that adding header files and other trivial code is up to you.

2              The Basic Application

The basic application is built from the Qt4 Gui Application template. Create such a project and include the QtCore and QtGui modules. Base the skeleton on the QMainWindow class. In this lab, the project is called TextEditor.



The resulting project consists of the following files:

  • TextEditor.pro – the project definition file.
  • mainwindow.ui – the user interface for the MainWindow class.
  • mainwindow.cpp/h – the implementation and declaration of the MainWindow class.
  • main.cpp – the main function. Gets everything initialized and started.

Review these files and verify that the project builds and runs.

                         Adding User Interface Elements

The next step is to add a QTextEdit widget and the basic user operations New, Close and Exit to the main window.

First of all, we need to add an icon resource to the project in question. This is because the icons will be used in the user interface which you are about to design.

Add a new Qt Resource file to the project. Name it icons.qrc. Open the resulting file, add the prefix icons and add the *.png files for this lab.

Now you are ready to start working on the user interface. Start by opening the mainwindow.ui file in Designer mode.

 

Add a QTextEdit widget to the main window and lay it out. Select the window itself to alter the margins of the layout to zero to get the new widget to fill the central area fully.

         

When the widget is in place you can try the dialog using ToolsForm EditorPreview (Ctrl+Alt+R).

The user operations are represented by QAction objects. These are administered through the Action Editor shown below.



Create the following actions.

Text

Name

Icon

Shortcut

ToolTip

New

actionNew

Ctrl+N

Create a new document

Close

actionClose

 

Ctrl+W

Close current window

Exit

actionExit

 

 

Exit application

 

Actions are added to the user interface through drag and drop. Drag the actionNew action onto the toolbar.

Add the entry File to the menu bar. Click on the type here text and enter File. Now drag and drop the actionNew, actionClose and actionExit to the menu. Add separators between each item by dragging the separator into position (you find it on the menu you are editing).

If you enter “&File” as the title of the menu you get a shortcut, “File”, automatically. To get an ampersand in a menu text, enter “&&”.

                          

                         Implementing Functionality

Each of the actions added need to be implemented. You do this by right clicking on the action in question to bring up its context menu. From the menu, pick the Go to slot option and pick the triggered() signal from the list that pops up.



Start by implementing the actionNew, this brings you to the following source code.

void MainWindow::on_actionNew_triggered()

{

 

}

In this slot, create a new MainWindow object on the heap and show it.

The other two actions, actionClose and actionExit, already have matching slots implemented by Qt. Make the following connections in the MainWindow constructor, after the setupUi call.

Source

Signal

Destination

Slot

actionClose

triggered()

this (current window)

close()

actionExit

triggered()

qApp (current application)

closeAllWindows()

 

Also, make sure to call setAttribute and set the Qt::WA_DeleteOnClose attribute in the MainWindow constructor.

                         Self Check

  • Ensure that you can open new windows.
  • Ensure that FileClose closes the current window.
  • Ensure that FileExit closes all windows and thus terminates the application.
  • Explain why the Qt::WA_DeleteOnClose attribute is set. Where does it come into play and why is that important?

 

3              Modifying and Closing

Now, the user can close a window with unsaved changes by mistake. That is not the expected behavior. Instead, the user expects to be prompted if risking to lose unsaved work. A two stage solution is needed to address this. First, the modified status of the document needs to be monitored. Second, the user must be prompted when trying to close a window with a modified document.

By monitoring the QTextEdit's textChanged signal document modifications can be tracked. Create a private slot called documentModified in your MainWindow class. In the constructor, connect the editor's signal to your slot. In the slot, set the windowModified property to true.

When reading the documentation, the windowModified property is not found in the QMainWindow class. Instead, it is defined in the QWidget class which QMainWindow inherits. You spot this by reading the properties section of QMainWindow, where “...properties inherited from QWidget” are mentioned.

The windowModified property interacts with the windowTitle property. For Mac OS X, the modified state is indicated by a dot in the window's red (left-most) button. On most other platforms, an asterisk in the title indicates that the document has been modified. This asterisk can be added to the windowTitle as “[*]”. Then Qt will synchronize the windowModified property and the windowTitle automatically.



To use this, set the windowTitle to “TextEditor[*]” in the MainWindow constructor and verify that modifications to the document triggers the document modified indication.

The other half of the solution is to prompt the user when the document is closed. This is done by re-implementing the protected closeEvent method of MainWindow. Start this by adding the function to your class declaration.

protected:

    void closeEvent(QCloseEvent *e)

Then add an empty function body in your class implementation.

void MainWindow::closeEvent(QCloseEvent *e)

{

 

}

 

The event, e, can be accepted or ignored using e->accept() and e->ignore(). Implement the function so that the event is accepted for all unmodified documents. If the document is modified, use the QMessageBox::warning function to prompt the user and accept or ignore accordingly.

The warning method takes the following arguments (look in the documentation if you need the actual types).

...

Zgłoś jeśli naruszono regulamin