L1-L4 Lab.pdf
(
493 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.
3h
Duration:
© Nokia Corporation. All rights reserved.
Qt in Education
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.
© Nokia Corporation. All rights reserved.
Qt in Education
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
mainwindow.ui
– the project definition file.
– the implementation and declaration of the
class.
– the user interface for the
MainWindow
class.
MainWindow
mainwindow.cpp
/
h
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.
© Nokia Corporation. All rights reserved.
Qt in Education
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
Tools
–
Form Editor
–
Preview
(Ctrl+Alt+R).
The user operations are represented by
through the
Action Editor
shown below.
QAction
objects. These are administered
Create the following actions.
Text
New
Close
Exit
Name
actionNew
actionClose
actionExit
Icon
Shortcut
Ctrl+N
Ctrl+W
ToolTip
Create a new document
Close current window
Exit application
actionNew
Actions are added to the user interface through drag and drop. Drag the
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 “
&&
”.
© Nokia Corporation. All rights reserved.
Qt in Education
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
actionClose
actionExit
Signal
triggered()
triggered()
Destination
this
qApp
Slot
close()
closeAllWindows()
(current window)
(current
application)
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
File
–
Close
closes the current window.
Ensure that
File
–
Exit
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?
© Nokia Corporation. All rights reserved.
Plik z chomika:
m_r_k
Inne pliki z tego folderu:
L1-L4 Lab.doc
(517 KB)
L1-L4 Lab.pdf
(493 KB)
L1-L4 Lab.odt
(437 KB)
L1-L4 Lab Source code.zip
(9 KB)
Inne foldery tego chomika:
L1 - The ideas behind Qt and a live demo
L2 - The Qt object model and the signal slot concept
L3 - Widgets and layouts
L4 - Datatypes collections and files
Zgłoś jeśli
naruszono regulamin