Post on 22-Jun-2015
description
Introduction to Qtprogramming
Dragoş Acostăchioaiehttp://www.unixinside.orgdragos@unixinside.org
Şcoala de vară “Informatică la castel”, Macea, 25 -31 august 2014
Dragoş Acostăchioaie - http://www.unixinside.org
Qt. Code less. Create more. Deploy everywhere
✗ Qt - cross-platform application and User Interface framework
✗ supports a lot of hardware and software platforms: desktop, mobile and embedded
✗ no need to rewrite the source code
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
Dragoş Acostăchioaie - http://www.unixinside.org
Qt. Code less. Create more. Deploy everywhere
Desktop platforms:✗ Linux/X11, other UNIX flavours (eg FreeBSD, Solaris, HP-UX, AIX)✗ Linux/Wayland✗ Mac OS✗ Windows
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
Dragoş Acostăchioaie - http://www.unixinside.org
Qt. Code less. Create more. Deploy everywhere
Mobile platforms:✗ Android✗ iOS✗ BlackBerry
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
Dragoş Acostăchioaie - http://www.unixinside.org
Qt. Code less. Create more. Deploy everywhere
✗ Embedded platforms:✗ Embedded Android✗ Embedded Linux✗ Windows Embedded, Windows CE, WinRT✗ Real-time operating systems (eg QNX, VxWorks)
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
Dragoş Acostăchioaie - http://www.unixinside.org
Qt. Code less. Create more. Deploy everywhere
✗ can interact with other languages (language bindings): Java (QtJambi), Ruby, C#, C++11
✗ scripting languages supported: QML (native), JavaScript, Phyton
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
Dragoş Acostăchioaie - http://www.unixinside.org
Qt. Code less. Create more. Deploy everywhere
IDEs supported:✗ Qt Creator✗ Eclipse✗ Microsoft Visual Studio
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
Dragoş Acostăchioaie - http://www.unixinside.org
Qt. Code less. Create more. Deploy everywhere
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ Write once, run everywhere - the same source code runs without changes ondifferent platforms (see also the porting paradigm of FLOSS solutions)
✗ Superior performances than languages running in a virtual machine (eg Java) – thebinary code is executed natively, resulting a much less resource consumption
✗ Uses the natively graphic system of the operating system – the applications havethe aspect and behaviour of the host system
✗ Porting from an operating system to another does not require source code changes – justcompile on the host platform (if no specific functionalities or libraries has been used)
✗ Ensure the source code against future host platform changes – at source or binary level;the developers can focus on application development, not on API changes
✗ Different markets can be easily achieved – developers can target multiple OSes
✗ Less source code maintenance costs
Dragoş Acostăchioaie - http://www.unixinside.org
Qt5
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
First Qt5 version launched in 2012
Dragoş Acostăchioaie - http://www.unixinside.org
Qt5
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ Modular – increase cross-platform portability✗ Qt Essentials✗ Qt Add-Ons✗ Qt Tools
✗ Consolidation of Qt Platform Abstraction
Dragoş Acostăchioaie - http://www.unixinside.org
Qt5
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
Compatible with Qt 4
Dragoş Acostăchioaie - http://www.unixinside.org
Qt5 – A modular architecture
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
Essentials
✗ Qt Core - provides core non-GUI functionality✗ Qt GUI - provides the basic enablers for graphical applications✗ Qt Multimedia - provides audio, video, radio and camera functionality✗ Qt Network - provides classes to make network programming easier and portable✗ Qt QML - classes for QML and JavaScript languages✗ Qt Quick - provides classes for embedding Qt Quick2 in Qt/C++ applications✗ Qt SQL - provides a driver layer, SQL API layer, and a user interface layer for SQL db✗ Qt Test - classes for unit testing Qt applications and libraries✗ Qt WebKit - provides a web browser engine (WebKit2)✗ Qt Widgets - extends Qt GUI with C++ widget functionality
Dragoş Acostăchioaie - http://www.unixinside.org
Qt5 – A modular architecture
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
Add-ons
✗ Qt Bluetooth – enables basic Bluetooth operations✗ Qt D-Bus - Inter-Process Communication using the D-Bus protocol (UNIX-only)✗ Qt Graphical Effects - graphical effects for use with Qt Quick 2✗ Qt Image Formats - plugins for additional image formats✗ Qt OpenGL - offers classes that make it easy to use OpenGL✗ Qt Positioning - provides positioning information✗ Qt Print Support - provides classes to make printing easier and portable✗ Qt Sensors - Provides classes for reading sensor data✗ Qt Serial Port - classes that enable access to a serial port✗ Qt Script - provides classes for making Qt applications scriptable✗ Qt Script Tools✗ Qt SVG - provides functionality for handling SVG images✗ Qt XML - implementations of SAX and DOM✗ Qt XML Patterns - support for XPath, XQuery, XSLT and XML schema validation
Dragoş Acostăchioaie - http://www.unixinside.org
Qt5 – A modular architecture
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
Tools
✗ Qt Designer – classes for extending Qt Designer✗ Qt Help - classes for integrating online documentation into applications✗ Qt UI Tools - classes to handle the forms created in Qt Designer
Dragoş Acostăchioaie - http://www.unixinside.org
Qt5 – A modular architecture
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
Future releases may include more modules
Dragoş Acostăchioaie - http://www.unixinside.org
What's new in Qt5
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ ✗ Efficient alternative to classical object communication (event listeners)
✗ Slots are member functions of the classes (inherited from Qobject),
which are called as answer for a specific signal
✗ There a pre-defined set of signals and slots
✗ A signal can be connected to multiple slots
✗ A slot can be connected to multiple signals
✗ A signal can be connected to another signal
✗ A signal is emmited when an event occurs
✗ production-ready ports for Android and iOS (since Qt 5.2)
✗ increase the graphic perfomances (OpenGL) up to 200%, due to
architectural changes
✗ better integration of Qt Quick and QML
✗ new features in Qt Quick:
✗ Canvas for drawing. Canvas provides an API that is similar to the HTML5 Canvas API, along with some additional features
✗ Shader Effects which enable GLSL shader programs to be integrated directly into QML code and applied to items and images
✗ Particle Effects for creating a variety of 2D particle systems
✗ Sprites can be used in animating 2D graphical objects and as a source for particle system
Dragoş Acostăchioaie - http://www.unixinside.org
What's new in Qt5
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ new features in Qt Quick (continued):
✗ Offline storage is a JavaScript API that conforms with the HTML5 Web Database API for storing data in Qt Quick applications
✗ Window provides a top-level window and the Screen type for accessing a screen's resolution and other details. This is useful for desktop applications developed using Qt Quick
✗ OpenGL-based rendering architecture for optimal performance
Dragoş Acostăchioaie - http://www.unixinside.org
What's new in Qt5
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ Qt Quick does reduce the effort considerably compared to the traditional native (C or C++) approach, but the new Qt Quick Controls and Qt Quick Layouts take it a step further. These new modules provide ready-to-use UI controls and layouts to enable faster application development and with less code
✗ Qt Quick Controls and Qt Quick Layouts provide a vast set of UI controls ranging from the most basic TextField and Button to the more complex TableView and TabView. These controls are also made available in Qt Quick Designer
✗ the new QtSensors and QtPositioning modules
✗ improved support for JavaScript and QML in Qt 5, while still having the C++ foundation and Qt Widget support. The JavaScript backend is powered by V8 and Qt WebKit 2 have full HTML5 support
Dragoş Acostăchioaie - http://www.unixinside.org
What's new in Qt5
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ Qt Multimedia provides a rich set of QML types and C++ classes to handle multimedia content. It also provides necessary APIs to access the camera and radio functionality. Qt 5 brings the Qt Multimedia module into the set of essential modules with support on all major platforms
✗ platform-specific extras (Qt Android Extras, Qt Mac Extras, Qt Windows Extras, Qt X11 Extras)
✗ Qt 5 splits the Qt libraries into domain specific libraries and modules. This means that an application can choose which libraries it requires and only compile, use and deploy those
✗ the Qt Widgets have been separated into their own module in Qt 5, the Qt Widgets module. It is part of the essential modules.
✗ the windowing system dependent parts of Qt have been completely rewritten for Qt 5, based on the Qt Platform Abstraction, a plugin architecture which allows Qt to dynamically load a windowing system integration based on what system it is running on.
Dragoş Acostăchioaie - http://www.unixinside.org
What's new in Qt5
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ Conectivity and networking improvements:
✗ provides better support for IPv6 and dual mode networks
✗ provides better ways to handle TCP socket-based connections and SSL certificates
✗ the opaque Private Keys support in Qt 5 enables applications to read private keys from devices such as PKCS#11 dongles for stricter client authentication
✗ the new Qt Bluetooth and Qt NFC new modules
Dragoş Acostăchioaie - http://www.unixinside.org
What's new in Qt5
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ JSON support:
✗ Qt applications that use simple data structures to store small amount of data, can now choose to use JSON documents over the complex database backends that need necessary drivers or plugins to access data
✗ the Qt Core provides a set of classes to enable parsing and generating JSON documents. These classes also allow you to convert between an in-memory binary representation of JSON to a standard textual JSON format.
Dragoş Acostăchioaie - http://www.unixinside.org
What's new in Qt5
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ Qt 5 extends the multi-point touch input support to include Qt Quick, covering all the information about the touch points, including the touch position, pressure, and velocity
✗ the mouse event handling in Qt 5 has been improved from its predecessor. Applications have more control to handle mouse events in Qt Quick. The mouse area in a QML document can propagate the mouse wheel and click events and also ignore propagated events
✗ support for more mouse buttons on platforms where this is supported. This comes in handy for gaming applications developed using Qt
Dragoş Acostăchioaie - http://www.unixinside.org
QtWidget
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ ✗ Efficient alternative to classical object communication (event listeners)
✗ Slots are member functions of the classes (inherited from Qobject),
which are called as answer for a specific signal
✗ There a pre-defined set of signals and slots
✗ A signal can be connected to multiple slots
✗ A slot can be connected to multiple signals
✗ A signal can be connected to another signal
✗ A signal is emmited when an event occurs
✗ QtWidget - the base class of all user interface objects
✗ the atom of the user interface - it receives mouse, keyboard and other events from the window system, and paints a representation of itself on the screen
✗ every widget is rectangular
✗ a widget without a parent widget is always an independent window (top-level widget)
✗ non-window widgets are child widgets, displayed within their parent widgets
✗ most widgets in Qt are mainly useful as child widgets
Dragoş Acostăchioaie - http://www.unixinside.org
Signals and slots
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
Dragoş Acostăchioaie - http://www.unixinside.org
Signals and slots
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ Efficient alternative to classical object communication (event listeners)
✗ Slots are member functions of the classes (inherited from Qobject),
which are called as answer for a specific signal
✗ There a pre-defined set of signals and slots
✗ A signal can be connected to multiple slots
✗ A slot can be connected to multiple signals
✗ A signal can be connected to another signal
✗ A signal is emmited when an event occurs
Dragoş Acostăchioaie - http://www.unixinside.org
Signals and slots
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ Declaration of slots:
[ access_type ] slots:
[ return_type ] function_name (param1, …, paramN)
✗ Declaration of signals:
signals:
void function_name (param1, …, paramN)
✗ Connect signals to slots:
connect (sender, SIGNAL(signal_name()), receiver, SLOT(slot_name()));
✗ Emit a signal:
emit signal_name (param1, …, paramN)
Dragoş Acostăchioaie - http://www.unixinside.org
Signals and slots
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
#include <QObject>
class Counter : public QObject
{
Q_OBJECT
public:
Counter() { m_value = 0; }
int value() const { return m_value; }
public slots:
void setValue(int value);
signals:
void valueChanged(int newValue);
private:
int m_value;
};
void Counter::setValue(int value)
{
if (value != m_value) {
m_value = value;
emit valueChanged(value);
}
}
Dragoş Acostăchioaie - http://www.unixinside.org
Template-based container classes
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ classes can be used to store items of a specified type
✗ lighter, safer, and easier to use than the STL containers
✗ implicitly shared, they are reentrant, and they are optimized for speed, low memory consumption, and minimal inline code expansion
✗ thread-safe
✗ two types of iterators - Java-style iterators and STL-style iterators
Dragoş Acostăchioaie - http://www.unixinside.org
Template-based container classes
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ QList - stores a list of values of a given type, that can be accessed by index
✗ QLinkedList - similar to QList, except that it uses iterators rather than integer indexes to access items
✗ QVector - stores an array of values of a given type at adjacent positions in memory
✗ QStack - subclass of QVector that provides "last in, first out" (LIFO)
✗ QQueue - subclass of QList that provides "first in, first out" (FIFO) semantics
✗ QSet - provides a single-valued mathematical set with fast lookups
✗ QMap - provides a dictionary (associative array) that maps keys to values
✗ QMultiMap - subclass of QMap that provides maps where one key can be associated with multiple values.
✗ QHash - similar to QMap, but provides significantly faster lookups
✗ QmultiHash - subclass of QHash that provides multi-valued hashes
Dragoş Acostăchioaie - http://www.unixinside.org
QString
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ QString – enhance working with strings
✗ built-in Unicode support
✗ lot of useful methods (arguments, concatenation, formatting, splitting,
translation, etc)
✗ cheap copying
Dragoş Acostăchioaie - http://www.unixinside.org
Qt Widgets
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ QCheckBox
✗ QComboBox
✗ QDateEdit
✗ QLabel
✗ QLineEdit
✗ QMenu
✗ QProgressBar
✗ QPushButton
✗ QRadioButton
✗ QScrollBar
✗ QSpinBox
✗ QTabWidget
Dragoş Acostăchioaie - http://www.unixinside.org
Qt Advanced Widget Classes
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ QColumnView
✗ QListView
✗ QTableView
✗ QTreeView
✗ QDesktopWidget
✗ QCalendarWidget
Dragoş Acostăchioaie - http://www.unixinside.org
Qt Widget Classes
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ Abstract Widget Classes (eg Qdialog, QabstractButton, Qframe)
✗ Organizer Widget Classes (eg QgroupBox, Qsplitter, QtabWidget)
✗ Graphics View Classes (eg QgraphicsScene)
✗ Model/View Classes (eg QmodelIndex, QitemSelectionModel)
✗ Main Window and Related Classes (eg QmainWindow, QMenu)
✗ Widget Appearance and Style-Related Classes (eg Qfont, Qstyle)
✗ Layout Classes (eg QboxLayout, QgridLayout, QStackedLayout)
Dragoş Acostăchioaie - http://www.unixinside.org
Download & install Qt
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ Online or offline installer
✗ Desktop: Linux, Mac, Windows installers
✗ Windows: Visual Studio and MinGW (included) versions
✗ Mobile: Android, iOS installers
✗ http://qt-project.org/downloads
Dragoş Acostăchioaie - http://www.unixinside.org
Qt Examples. Hello, world!
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
#include <QApplication>#include <QLabel>
int main( int argc, char **argv ){
QApplication app( argc, argv );
QLabel l( "Hello, world!" );
l.show();
return app.exec();}
Dragoş Acostăchioaie - http://www.unixinside.org
Qt Examples. A text editor
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ use Qt Creator to create a project with the necessary files, but we could
also write all the code ourselves
✗ use Qt Designer to create the user interface
Dragoş Acostăchioaie - http://www.unixinside.org
Qt Examples. A text editor
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
<?xml version="1.0" encoding="UTF-8"?><ui version="4.0"> <class>Notepad</class> <widget class="QMainWindow" name="Notepad"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>Notepad</string> </property> <widget class="QWidget" name="centralWidget"> <widget class="QWidget" name=""> <property name="geometry"> <rect> <x>70</x> <y>0</y> <width>268</width> <height>235</height> </rect> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QTextEdit" name="textEdit"/> </item> <item> <widget class="QPushButton" name="quitButton"> <property name="text"> <string>Quit</string> </property> </widget> </item> </layout> </widget> </widget> <widget class="QMenuBar" name="menuBar"> ...
✗ the user interface is stored in a file using XML
Dragoş Acostăchioaie - http://www.unixinside.org
Qt Examples. A text editor. Main source file
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
#include "notepad.h"#include <QApplication>
int main(int argc, char *argv[]){ QApplication a(argc, argv); Notepad w; w.show();
return a.exec();}
Dragoş Acostăchioaie - http://www.unixinside.org
Qt Examples. A text editor. The header file
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
#include <QMainWindow>
namespace Ui {class Notepad;}
class Notepad : public QMainWindow{ Q_OBJECT
public: Notepad(QWidget *parent = 0); ~Notepad();
private slots: void on_quitButton_clicked();
private: Ui::Notepad *ui;};
✗ the class declaration contains the Q_OBJECT macro
✗ It must come first in the class definition
✗ the Q_OBJECT macro declares our class as a Qobject
✗ the value 0 for parent argument in calling Notepad constructor means the widget has no parent (is a top-level widget)
✗ QtDesigner connects the button's clicked() signal to a slot in the Notepad class
✗ we only need to declare a slot with a name that follows a standard convention
Dragoş Acostăchioaie - http://www.unixinside.org
Qt Examples. A text editor. The source file
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
#include "notepad.h"#include "ui_notepad.h"
Notepad::Notepad(QWidget *parent) : QMainWindow(parent), ui(new Ui::Notepad){
ui->setupUi(this);}
Notepad::~Notepad(){ delete ui;}
void Notepad::on_quitButton_clicked(){ qApp->quit();}
✗ the header and source files has been generated by the wizard
✗ the ui_notepad.h file has been generated by the uic tool
Dragoş Acostăchioaie - http://www.unixinside.org
Qt Examples. A text editor. The source file
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ Qt Designer adds the private slots on_actionOpen_triggered() and on_actionSave_triggered()
✗ we will implement these functions in notepad.h
Dragoş Acostăchioaie - http://www.unixinside.org
Qt Examples. A text editor. The source file
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
void Notepad::on_actionOpen_triggered(){ QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(), tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));
if (!fileName.isEmpty()) { QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { QMessageBox::critical(this, tr("Error"), tr("Could not open file")); return; } QTextStream in(&file); ui->textEdit->setText(in.readAll()); file.close(); }}
✗ the static getOpenFileName() function displays a modal file dialog. It returns the file path of the file selected, or an empty string if the user canceled the dialog
✗ we try to open the file with open(), which returns true if the file could be opened
✗ we read the data using the QTextStream class, which wraps the QFile object. The readAll() function returns the contents of the file as a Qstring
✗ we use the function tr() for visibile strings to provide translations
Dragoş Acostăchioaie - http://www.unixinside.org
Qt Examples. A text editor. The source file
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
void Notepad::on_actionSave_triggered(){ QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QString(), tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));
if (!fileName.isEmpty()) { QFile file(fileName); if (!file.open(QIODevice::WriteOnly)) { // error message } else { QTextStream stream(&file); stream << ui->textEdit->toPlainText(); stream.flush(); file.close(); } }}
✗ we write the contents of the text edit to the file, using the QTextStream class
✗ QTextStream can also write QStrings to the file with the << operator
Dragoş Acostăchioaie - http://www.unixinside.org
Qt Examples. A text editor. The project file
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = NotepadTEMPLATE = app
SOURCES += main.cpp\ notepad.cpp
HEADERS += notepad.h
FORMS += notepad.ui
✗ the project file specifies: the application name, the forms, header and source files used by the application
✗ the QT argument specifies the modules used by the application
Dragoş Acostăchioaie - http://www.unixinside.org
Qt Examples. A text editor.Building and running the project
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ build and run the project in QtCreator
✗ we can also use at the command line:
✗ qmake – reads the project file and produces a Makefile with instructions
✗ make (or nmake on Windows)
Dragoş Acostăchioaie - http://www.unixinside.org
Resources
Şcoala de vară “Informatică la castel”, Macea, 25 – 31 august 2014
✗ Qt documentation - http://qt-project.org/doc/
✗ Mark Summerfield books - http://www.qtrac.eu/marksummerfield.html
✗ Learning Qt videos - https://www.ics.com/learning/icsnetwork