and the Qt Overview listed in the
Resources section for more information.
Qt Assistant, discussed later in this
article, also has an excellent tutorial.
Adding Your Own Code
Other Qt4 tutorials become code-heavy
at this point and lose people who are
not experienced C++ programmers.
Here, I explain how to link your code
to the GUI interface in a simple and
pain-free manner with minimal C++.
Adding code to a GUI project is not
like writing code for a command-line
program. Your code must be placed in
the class created for you, in the example
demo_1.cpp, not a main() routine.
You should alter only demo_1.cpp,
demo_1.h or demo_1.ui. The other files
are generated automatically and will be
overwritten on the next build.
Your code can be started only when
a GUI event happens and it is linked to
your code, or when a timer you have
created times out. Furthermore, your
code must do its job quickly and return.
If your code delays, the entire application freezes and stops responding to
user stimuli. These factors require you to
design your code differently from how
you would design a command-line program. When you write a GUI program,
plan for all of your code to respond to
GUI events and timers. The GUI package
handles the rest for you.
Most GUI systems have a library of
useful classes, and it’s very much worth
learning how to use those classes, as
they can save you a lot of time and pro-
vide features you could not implement
otherwise. The library with Qt Designer
is particularly powerful and worth
mastering (more on that later).
Listing 1. Adding Slots to demo_1.h
#include <QtGui> public: demo_1(QWidget *parent = 0); ~demo_1();
private: Ui::demo_1Class ui;
//--- add the following lines. QTimer timer ;
public slots: void on_pushButton_clicked(); void on_pushButton_ 2_clicked(); void on_actionSave_triggered() ; void timer_tick() ; };