Vous êtes sur la page 1sur 17

Some Basis of Qt Programming

Hanyu Liu
School of Computing, USM
Outline

● What is Qt
● Event Loop
● SIGNAL and SLOT
● OpenGL in Qt
– QGLWidget
– QGraphicsView
● Further Study
What is Qt
● The Qt toolkit is a multi-platform C++ GUI toolkit (class
library)
● 1991, Qt is first developed by Troll Tech.
● 1995, Qt was released in both commercial and non-
commercial GNU licenses.
● 1997, Qt was chosen as the code basis for the KDE Linux
desktop environment
● 2001, Qt is available on Windows, Unix, Linux, Embedded
Linux and Mac OS X.
● 2008, Nokia acquire Qt and release Qt for Symbian
Why GUI Toolkit?
● Operating system( OS ) allows you to draw primitive graphics.
– Lines, rectangles, set foreground/background colors, and interact with the
user and send events back to the OS.
● Programming graphical objects like buttons, scroll bars, dialog boxes,
and tool bars is very difficult using pure OS APIs.

The developers need to


handle the events both
from the buttons and
the objects in the game
What does Qt provide
GUI Emulation, Complete API written on top of
primitive graphics (i.e. supported by all GUI OS). Each
widget in Qt is drawn by the toolkit.
Advantages:
Faster because toolkit layer talks directly to OS.
Easy to change display style from within application – Motif style under Windows, or
Windows style under Unix.
Widgets are implemented as C++ classes and can be inherited from directly in client code.
(This is the reason Qt why is chosen for KED instead of GTK, which is written in C)

Disadvantages:
Does not support porting of existing programs (i.e. no Word under Unix).
More work to create implementations for every OS release for every target platform. This
means Qt support lags behind OS releases. Impacts not only Qt but any widgets created by
user
Emulation of look-and-feel is not 100% exact.
Setup environment by using
Visual studio 2008
Qt plug in for Visual Studio
Qt 4.6 for Visual Studio
Event Loop
● Definition: An event loop is a programming construct that
waits for and dispatches events or messages in a program.
● Most modern applications use a main loop to wait events,
which mainly are provided by operating system.
● The main loop are often blocked by the event function until
an event is available. Thus, the loop is enabled only when it
is necessary.
display=init_Display(); //initialize a display
construct_interface(); //draw the components on the display
While (PROGRAM_RUN==true){ //enter the event loop
if(receiveEvent(display)) //if there is an event
process_Event(); //then process the event and perform a proper reaction
}
Hello World
Qt C++
#include <QApplication> #include <iostream>
#include <QObject> using namespace std;

#include <QPushButton> int main(int argc, char **argv)


{

int main(int argc, char **argv) cout<<”Hello”


cout<<” World!”
{
cout<<endl;
QApplication myapp(argc, argv);
return 0;
QPushButton* button = new
QPushButton( QObject::tr("Hello }
world") ); Output the charters and exit with a return value

button->show();
What happens after running each code?
return myapp.exec();
} Display the window and refresh the window.
If we do not stop the program manually,
it will not stop.
Example 1
Widgets Of Qt
● Widgets are visual elements that are combined to
create user interface. Buttons, menus, scroll bars,
message boxes, and application windows are all
examples of widgets.
● Properties
– width, height, mouseTracking, font,
backgroundColor, etc.
● Virtual functions
– moveEvent(), paintEvent(),
keyPress(Release)Event(), resizeEvent(), etc.
Layout in Qt
● Qt includes a set of layout management classes that
are used to describe how widgets are laid out in an
application's user interface.
● Typical Layouts are horizontal ( QHBoxLayout ),
vertical ( QVBoxLayout ), and grid( QGridLayout )
layout.
Qt Designer
● Qt designer is a tool for designing and building GUI
from Qt components.
● Components created with Qt Designer can also take
advantage of Qt's signals and slots, and they can be
previewed so that you can ensure that they will look
and feel exactly as you intended.
QGLWidget
● A subclass of QWidget.
● QGLWidget provides functionality for displaying
OpenGL graphics integrated into a Qt application.
● Not only keep the properties and memberfunctions
from QWidgt, but also provide virtual functions for
OpenGL programming:
– paintGL(): renders the OpenGL scene;
– resizeGL(): sets up viewport, projection and
– keyPressEvent(): inherited from Qwidget, called
when a key is pressed.
– ......
Signals and Slots
● Signals and slots are used for communication
between objects. All classes that inherit from
QObject or one of its subclasses(e.g., QButton and
QWindow) can contain signals and slots.
– A signal can be connected to as many slots as you
need. It is even possible to connect a signal to
another.
– Without assigning signals, a slot can be used as a
normal member function.
● Advantage:
Disadvantage:
– Type safe. Slow
– Loosely coupled.
Signals and Slots
Signals and Slots
● Qt meta object compiler (moc)
It parses C++ header files and generates C++ code
necessary for Qt to handle signals and slots. The
signals, slots and emit keywords are macros, so the
compiler preprocessor changes or remove them.
● How to do
– moc -o moc_file.moc moc_file.cpp moc_file.h
– #include “moc_file.moc”
Fortunately, the Qt utility “qmake” takes care of all
this.
Further Study
● All documentation is
available through the
trolltech web site.
● Qt Assistant is a Qt help
browser. It has
searching and indexing
features that make it
faster than the web.
● Qt creator is an IDE for
GUI development and
available on Windows,
Linux and Mac.

Vous aimerez peut-être aussi