Sergio Benini
Department of Information Engineering Faculty of Engineering University of Brescia Via Branze, 38 – 25231 Brescia - ITALY
Surface and Volumetric Data
Rendering and Visualisation
THE Qt TOOLKIT
1 Università degli Studi di Brescia - 2011/12
What is Qt?
• “Qt is a cross platform development framework written in C++.”
• C++ framework – bindings for other languages
– Python, Ruby, C#, etc.
• Cross platform applications built from one source
• Originally used for interfaces, now for everything
– Databases, XML, WebKit, multimedia, networking, OpenGL, scripting,
non-GUI...
Sergio Benini
What is Qt?
3 Università degli Studi di Brescia - 2011/12
• Qt is made up of modules
– All modules have a common scheme and are built from the same API
design ideas
Desktop target platforms
• Windows
Sergio Benini
Embedded target platforms
5 Università degli Studi di Brescia - 2011/12
• Windows CE
• Symbian
• Maemo
• Embedded Linux
The history of Qt
• 1991 – Haavard Nord and Eirik Chambe-Eng begin to develop what will be
Qt supporting X11 and Windows
• 1993 – They produced Qt's first graphics kernel and could implement
their own widget
• 1994 – The company Trolltech was formed
• 1996 – The KDE project was started by Matthias Ettrich (now works for
Nokia Qt Development Frameworks)
• 2001 – Added support for Mac OS X
Sergio Benini
The Qt Community
7 Università degli Studi di Brescia - 2011/12
• QtCentre (www.qtcentre.org)
– Forum, news, wiki
• Qt labs (labs.trolltech.com)
– developer blogs, research projects
• #qt at freenode
– IRC channel, has wiki at qtnode.net
• Mailing lists (lists.trolltech.com)
Getting Qt
• Installers and snapshots are downloaded from
– qt.nokia.com/downloads
Sergio Benini
Windows Installation
9 Università degli Studi di Brescia - 2011/12
• 1.Download the Qt SDK for Windows
• 2.Run the downloaded installer
• 3.Click through the installer
Mac OS X installation
• 1.Download the Qt SDK for Mac OS X
• 2.Run the downloaded installer package
• 3.Click through the installer
Sergio Benini
X11 installation
11 Università degli Studi di Brescia - 2011/12
• If possible, use the package manager from your distribution
– (K)ubuntu – qt-sdk from universe
– Debian – qtcreator
– OpenSUSE – qt-creator
– Gentoo – qt-creator
– Arch Linux – qt qt-doc qt-creator
– ...
X11 installation
• 1.Download the Qt SDK for your Linux version
• 2.Make the installer executable
chmod u+x qt-sdk-linux-*.bin
3.Run the installer and click your way through it
Sergio Benini
Hello World
13 Università degli Studi di Brescia - 2011/12
• Open Qt Creator
Hello World
Sergio Benini
Hello World
15 Università degli Studi di Brescia - 2011/12
#include <QApplication> #include <QWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv); QWidget window;
window.resize(250, 150);
window.setWindowTitle("Hello World!"); window.show();
return app.exec(); }
Hello World
#include <QApplication>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv); QWidget window;
window.resize(250, 150);
window.setWindowTitle("Hello World!"); window.show();
We include necessary header files.
Sergio Benini
Hello World
17 Università degli Studi di Brescia - 2011/12
#include <QApplication> #include <QWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.resize(250, 150);
window.setWindowTitle("Hello World!"); window.show();
return app.exec(); }
This is the application object. Each application programmed in Qt4 must have this object.
Hello World
#include <QApplication> #include <QWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.resize(250, 150);
window.setWindowTitle("Hello World!"); window.show();
Sergio Benini
Hello World
19 Università degli Studi di Brescia - 2011/12
#include <QApplication> #include <QWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv); QWidget window; window.resize(250, 150); window.setWindowTitle("Hello World!"); window.show(); return app.exec(); }
Here we resize the widget. Set a title for our main window. In this case, the QWidget is our main window. And finally show the widget on the screen.
Hello World
#include <QApplication> #include <QWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv); QWidget window;
window.resize(250, 150);
window.setWindowTitle("Hello World!"); window.show();
We start the main loop of the application.
Sergio Benini
Hello World
21 Università degli Studi di Brescia - 2011/12
The QObject
• QObject is the base class of almost all Qt classes and all widgets
– Exception:
• Classes that need to be lightweight such as graphical primitives
• Data containers (QString, QList, QChar, etc)
• Classes that needs to be copyable, as
QObjects cannot be copied
• It contains many of the mechanisms that make up Qt
– events
Sergio Benini
Memory Management
23 Università degli Studi di Brescia - 2011/12
• QObject can have parent and children
– When a parent object is deleted, it deletes its children
#include <QApplication>
#include <QWidget>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *parent = new QWidget;
parent->resize(250, 150);
parent->setWindowTitle("Hello World!");
QLabel *mylabel = new QLabel("Hello World!!!",parent);
mylabel->move(80,60); parent->show();
return app.exec(); }
Memory Management
• QObject can have parent and children
Sergio Benini
Signals and Slots
25 Università degli Studi di Brescia - 2011/12
• The signals and slots are what makes the different Qt components as reusable as they are.
• They provide a mechanism through which it is possible to expose interfaces that can be freely interconnected (a menu item, push button, tool-bar button, …, can expose signal corresponding to "activated", "clicked" or any other
appropriate event).
• By connecting such a signal to a slot of any other item, the event automatically calls the slots.
• The key advantage of the signals and slots is that the caller does not have to know anything about the receiver and vice versa.
• You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you need. It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately
Signals and Slots
• In order to be able to use the signals and slots each class has to be declared in a header file. The implementation is best placed in a separate cpp file.
• The header file is then passed through a Qt tool known as the moc. The moc produces a cpp containing the code that makes the signals and slots happen (and more).
• The figure illustrates this flow. Notice the naming convention used (the moc_ prefix) in the figure.
Sergio Benini
Signals and Slots
27 Università degli Studi di Brescia - 2011/12
• Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them.
A More Complex Example
Sergio Benini
A More Complex Example
29 Università degli Studi di Brescia - 2011/12
A More Complex Example
Sergio Benini
A More Complex Example
31 Università degli Studi di Brescia - 2011/12
A More Complex Example
Sergio Benini
A More Complex Example
33 Università degli Studi di Brescia - 2011/12
• 5 Files: widget.cpp and widget.h files We talk about them later→ • 5 Files: widget.ui double click on it: it should start the Qt Designer→
A More Complex Example
• Add a Text Browser (double-click it for setting the default text)
• Add 2 Push Buttons (call them through the inspector my_button and quit)
Sergio Benini
A More Complex Example
35 Università degli Studi di Brescia - 2011/12
• widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> namespace Ui { class Widget;}
class Widget : public Qwidget { Q_OBJECT
public:
explicit Widget(QWidget *parent = 0); ~Widget(); public slots: void rewrite_text(); void quit(); private: Ui::Widget *ui;}; #endif // WIDGET_H
We define the slots that we want to be executed when a certain signal is emitted.
A More Complex Example
• widget.cpp
#include "widget.h"
#include "ui_widget.h"
void Widget::rewrite_text(){
ui->textBrowser->setText("You have clicked the button!!!");}
void Widget::quit(){ qApp->quit();}
Widget::Widget(QWidget *parent) : QWidget(parent),ui(new Ui::Widget){ ui->setupUi(this);
connect(ui->my_button, SIGNAL(clicked()),this,SLOT(rewrite_text())); connect(ui->quit, SIGNAL(clicked()),this,SLOT(quit()));
Sergio Benini
A More Complex Example
37 Università degli Studi di Brescia - 2011/12
• widget.cpp
And obviously the implementation of the slots.
#include "widget.h"
#include "ui_widget.h"
void Widget::rewrite_text(){
ui->textBrowser->setText("You have clicked the button!!!");}
void Widget::quit(){ qApp->quit();}
Widget::Widget(QWidget *parent) : QWidget(parent),ui(new Ui::Widget){ ui->setupUi(this);
connect(ui->my_button, SIGNAL(clicked()),this,SLOT(rewrite_text())); connect(ui->quit, SIGNAL(clicked()),this,SLOT(quit()));
}
Widget::~Widget(){ delete ui;}