Surface and Volumetric Data Rendering and Visualisation THE Qt TOOLKIT Department of Information Engineering Faculty of Engineering University of Brescia Via Branze, 38 25231 Brescia - ITALY 1
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... 2
What is Qt? Qt is made up of modules All modules have a common scheme and are built from the same API design ideas 3
Desktop target platforms Windows Mac OS X Linux/Unix X11 4
Embedded target platforms Windows CE Symbian Maemo Embedded Linux Direct framebuffer access 5
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 2005 All platforms released under GPL 2008 Nokia acquires Trolltech 2009 Support for S60 6
The Qt Community 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) Qt interest(still very active) 7
Getting Qt Installers and snapshots are downloaded from qt.nokia.com/downloads 8
Windows Installation 1.Download the Qt SDK for Windows 2.Run the downloaded installer 3.Click through the installer 4.Start QtCreator from the start menu 9
Mac OS X installation 1.Download the Qt SDK for Mac OS X 2.Run the downloaded installer package 3.Click through the installer 4.Start QtCreator from the Finder 10
X11 installation 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... 11
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 4.Start QtCreator from your desktop or menu 12
Hello World Open Qt Creator File New File or Project Other Project Empty Qt Project 13
Hello World File New File or Project C++ C++ Source file 14
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(); return app.exec(); } 15
Hello World #include <QApplication> #include <QWidget> We include necessary header files. 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(); } 16
Hello World #include <QApplication> #include <QWidget> This is the application object. Each application programmed in Qt4 must have this object. Except for console applications. 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(); } 17
Hello World This is our main widget. #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(); } 18
Hello World #include <QApplication> #include <QWidget> int main(int argc, char *argv[]) { QApplication app(argc, argv); 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. QWidget window; window.resize(250, 150); window.setwindowtitle("hello World!"); window.show(); return app.exec(); } 19
Hello World #include <QApplication> #include <QWidget> We start the main loop of the application. 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(); } 20
Hello World 21
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 signals and slots properties memory management 22
Memory Management 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(); } 23
Memory Management QObject can have parent and children When a parent object is deleted, it deletes its children 24
Signals and Slots 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 whenever the first is emitted.) 25
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. This additional compilation stage may seem to complicate the building process, but there is yet another Qt tool, qmake that makes it as simple to build any Qt application. This will be described in further on in this tutorial. 26
Signals and Slots Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal. 27
A More Complex Example File New File or Project QtWidget Project Qt GUI Application 28
A More Complex Example Base class: QWidget 29
A More Complex Example An empty Window... 30
A More Complex Example 5 Files: A.pro project file We don't need to change it :) 31
A More Complex Example 5 Files: A main.cpp file We don't need to change it :) 32
A More Complex Example 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 33
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) Optionally add layouts and spacers for a more precise localization of the objects You can already run the application, but it just do nothing :) 34
A More Complex Example 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(); We define the slots that we want public slots: void rewrite_text(); to be executed when a certain void quit(); signal is emitted. private: Ui::Widget *ui;}; #endif // WIDGET_H 35
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())); } Widget::~Widget(){ delete ui;} We define which slot is executed when the specified signal is emitted. 36
A More Complex Example widget.cpp #include "widget.h" #include "ui_widget.h" And obviously the implementation of the slots. 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;} 37
Documentation http://doc.qt.nokia.com/ 38