Introduction to Qt Build Great Apps Using Qt Jeff Alstadt Introduction  My name is Jeff Alstadt  I  work as a Senior Developer.

Download Report

Transcript Introduction to Qt Build Great Apps Using Qt Jeff Alstadt Introduction  My name is Jeff Alstadt  I  work as a Senior Developer.

Introduction to Qt
Build Great Apps Using Qt
Jeff Alstadt
Introduction
 My name is Jeff Alstadt
 I
 work as a Senior Developer at Litholink, a LabCorp
company
 am a passionate C/C++ and .NET developer
 worked with Qt in 2011 while at Centare
 love open source and believe it is the right way to do
software development
 am an avid technical speaker and blogger
Today we are talking about
This Is Qt 5
What is Qt?
 Qt is an open source cross platform UI toolkit for the
C++ platform
 Qt is used by over 500,000 developers
 Although digia has taken over support the Qt project,
Qt is still a strong contender for embedded Linux
(RTOS), desktop UI development (Windows, Mac and
Linux), and mobile devices (tablet, phone)
Qt Founders
 Qt was founded in 1991 in Oslo, Norway by Erik
Chambe-Eng and Haavard Nord
 Both Erik and Haavard had founded Trolltech on 4
March 1994.
 Trolltech‘s IPO came on the Oslo Stock Exchange in
July 2006
 In 2001 Qt introduced Qtopia which was an
application platform for embedded Linux-based
devices such as mobile phones, home media and etc.
Qt Founders
 The toolkit was called Qt becuase the letter Q looked
appealing in Haavard‘s Emacs font and “t“ was
inspired by Xt, the X toolkit
So what does the
Qt ecosystem look like
But isn’t Qt dead?
 Actually not, over 500,000 developers have applications
built on Qt technology
 Even though Nokia sold Qt to Digia, Digia has promised 15
M Euro investment in Qt research and development and
ecosystem activities
 Commercial licensing ensures future funding of Qt
development
 The next Blackberry will be using Qt as its UI framework
 Qt mobile apps have been successfully ported to iOS and
Android
Digia’s Goals for Qt






#1 in multi-platform support
#1 in developer experience
#1 in creating great user experiences
Belief in dual licensing model
Strong value generating ecosystem
Open business architecture
But Qt can’t handle web…
 Actually Qt is enabled for the web. Using Qt WebKit Qt
developers can develop apps using C++, QML and
HTML5/JavaScript.
 Qt WebKit provides facilities for rendering of HTML,
XHTML, SVG, CSS and JavaScript
 QtWebKit Bridge provides a bridge between the JavaScript
execution environment and the Qt object model.
 Qt Webkit is based on the Open Source WebKit Engine
Fundamental Technologies of Qt









The Tulip Container Classes
The Arthur Paint System
The Interview Framework
The Scribe Classes
Qt Main Window Classes
Graphics View
Qt Network Module
Resource System
Unit Testing Framework
Qt Development Tools
 Qt Creator – cross platform C++ debugger and developer designed
specifically for Qt
 Qt Visual Studio add-in – provides Qt integration with Visual Studio
2008, 2010 and 2012
 Qt Simulator - A fast and lightweight simulator for Qt applications
running on Nokia devices.
 Eclipse Integration Plugin
 Qt Teambuilder – distributed C/C++compilation
 Qt Mobility – mobile API
 Qt SDK
 Qt Quick
 Qt Webkit
Qt Lighthouse
 The Qt Lighthouse project was a project started to
gain advancement in providing Qt ports to iOS and
Android
 The Lighthouse project is the project that will allow
Qt to be deployed on mobile devices
Qt Meets Android
 The Necessitas project is a community driven
Lighthouse based port of Qt to Android
 It has a plugin for Qt Creator to do Android
development
 An installer application for Android called Ministro
exists
 Ministro is responsible for downloading the Qt libraries
specific to that Android device
Qt Meets iOS
 The Qt Lighthouse project was an initiative to get Qt
running on the iOS platforms.
Qt Overall Architecture
Let’s get technical with Qt
 Here we will get into Qt concepts









QObject
Meta Object Compiler (MOC)
Qt Container Classes
Signal Slot System
Qt Property System
Qt Event System
Qt Model View Concept
Qt Widgets (QWidget)
Qt Graphic View (QGraphicView)
QObject
 Most objects in Qt inherit from QObject
 This provides your custom Qobject classes to have




Signals and slots support
Property Support
Memory Management Support
Introspection
Meta Object Compiler
 A program that handles Qt’s C++ Extensions
 How it works:
 Moc looks for the Q_OBJECT macro
 Once found moc will produce a C++ source file containing the
meta-object code that is required for
 Signals and slot mechanism
 Run-time type information
 Dynamic Property System
 Moc produces another header and source file with the
moc_mycppfile.cpp
 These need to be included in the make file so that the code
can be compiled and linked.
Qt Container Classes
 Qt utilizes many of the STL collection classes and
introduces some of its own
 These include







QList
QMap
QStack
QQueue
QSet
QHashMap
QVector
Signals and Slots
 Specific concept in Qt every
Qt developer needs to know
 Implements simple Observer
Pattern
 Type safe
 Decoupled
 Objects inherited from
QObject will only receive the
signal notifcations
 connect connects the object
signal to the respective slot
Signal Slot Code Example
Qt Property System
The Qt system provides a means to
set any property in the Qt MetaObject System
The Qt solution works with any
standard C++ compiler Qt supports
Q_PROPERTY is a macro that is
used to declare property
This declaration must be done
inside a QObject
Qt Event System
 An event is an object or a set of objects derived from
the abstract class QEvent
 Events represent things that have happened either
within an application or as a result of outside activity
that the application needs to know about
 Events are recieved and handled by instance of a
derived QObject class
Common Qt Events





QResizeEvent (Window Resizing)
QPaintEvent (Window Repainting)
QMouseEvent (Mouse Input)
QKeyEvent
(Keyboard Input)
QCloseEvent (Window close)
Sending Events
 In Qt, we can utilize the QCoreApplication to create and
send our own custom events.
 To do this, we utilize sendEvent() and postEvent().
 sendEvent() processes the event immediatly.
 postEvent() posts the event on queue for later dispatch When
Qt‘s main event loop runs, it will then dispatch all posted
events.
 To create a custom Qt Event type, simpily subclass your
event from QEvent and ensure it has an event number that
is greater than QEvent::User (1000 presently in 4.8)
 For all events, check here
Model View
 Qt has an advanced Model View archictecture
concept that was inspired from the MVC approach
 Instead Qt utilizes a delegate instead of a controller
 The delegate is used to provide a fine control over
how items are rendered and edited
 For Qt specific views, Qt provides the appropriate
delegate for that view
Commonly Used Qt Model Classes







QStringListModel
QStandardItemModel
QDirModel
QSqlQueryModel
QSqlTableModel
QSqlRelationalTableModel
QSortFilterProxyModel
Graphics View
 QGraphicsView – a view for fast structured 2D graphics
 Graphics items known as QGraphicItems are placed in a
scene known as QGrahpicsScene
 A scene is then displayed in one or more views
(QGraphicViews)
 Graphic items can be
 lines, rectangesl polygons, pixmaps, ellipses, text, SVG
drawings, widgets or any customizable item you can think of
 moved, scaled, rotated
Graphics Items
 QGraphicsItems are lightweight compared to QWidget
 This allows for thousands of these guys to exist while not
taking a hit on performance or memory
 QGraphItems can be arbitrarily transformed (QTransform)
 Support for high quality zooming for example
 Graphics Items have there own internal event system that
is slightly different than the regular QEvent system
 Widgets can be embedded into a graphics scene by using
the QGraphicsProxyWidget
Hello World Graphics View Example
#include <QtGui>
int main(int argc, char **argv) {
QApplication app(argc, argv);
QGraphicsScene scn;
QGraphicsEllipseItem ellipse(-10, -10, 120, 50);
scn.addItem(&ellipse);
QGraphicsTextItem text(“Hello World”);
scn.addItem(&text);
}
QGraphicsView view(&scn);
view.show();
return app.exec():
For More About QGraphicView
Check these Videos:
 http://qtproject.org/videos/watch/programming_with_qt_qgr
aphicsview_by_kdab
 http://qtproject.org/videos/watch/programming_with_qt_qgr
aphicsview_by_ics
Internationalization





Qt supports Unicode via QString
Support for Right-to-Left (Chinese)
Read/Write files in different codecs
Layout system can be internationalized
Qt Linguist applications allows apps to be translated
easily
To learn more on Internationalization
see
 “Translating Qt Applications” – Benjamin Poulain
http://qtproject.org/videos/watch/translating_qt_applications
Interprocess Communication (IPC)




Shared memory
TCP/UDP Sockets (QSocket)
HTTP/FTP protocol support
D-Bus
Qt State Machine Framework (SMF)
 Introduced in Qt 4.6 and part of QtCore module
 Originated from Qt-SCXML research project
 SMF provides the means to create heirarchical finite
state machines (HFSMs)
 SMF also provides an “interpreter” for executing
HFSMs
 Helps to reduce spaghetti code by
 By making if statements implicit since state is implicit
 Producing clear control flow
For more learning on Qt State
Machines
 State Machine Framework
 http://www.slideshare.net/qtbynokia/qt-state-machineframework -Slides
 http://qtproject.org/videos/watch/state_machine_framework Video
Unit Testing
 Qt supports test driven development (TDD)
 Using QTestLib, Qt developers can do unit tests
 Qt supports






Basic Unit Testing
Data Driven Testing
Cross-Platform and Cross-Compiler Unit Tests
Stand alone unit tests
Memory Debugging
Integrations
Qt Unit Test Macros
 QVERIFY
 Evaluates complex expressions
 QCOMPARE
 Compares two values using the respective object
comparison operator
Hello World Unit Test Example
class QStringTest : public QObject {
Q_OBJECT
private slots:
void toUpper()
{
QString str(“hello world”);
QCOMPARE(str.toUpper(),
Qstring(“HELLO WORLD”));
}
};
Benefits of Data Driven Tests





Allows for separation of logic and data
Improved readability
Easily extendable
Reduction of copy and pasting of code in test code
Ease in testing the border cases of the application
Coded UI Test Support
 QtTestLib offers non-native mouse and keyboard
simulation support for QWidgets
 Very basic support for replay eventa
 3rd party tools such as Squish and KD Executer
Learn more about Qt Unit Testing
 Squish - http://qtproject.org/wiki/Category:Tools::Squish
 Unit Testing in Qt Applications by Harald Fernengel
http://qtproject.org/videos/watch/unit_testing_in_qt_applicati
ons
Performance
 Qt provides QBENCHMARK to measure code
 QBENCHMARK measures code based on




Walltime (default)
CPU Tick Count (-tickcounter)
Valgrind/Callgrind (-callgrind)
Event counter (-eventcounter)
Theory of Constraints
 TOC was developed by Eliyahu M. Goldratt
 Basically TOC is a theory which says that given a
complex system, there is usually one aspect of that
system that will limits it ability to achieve its goal or
optimal functioning. Before an achievement of any
significant improvement of the system can be
achieved, the constraint or bottleneck must be first
identified and resolved
 Reduce Bottlenecks improve Performance
Qt Optimization Techniques
 Choose the right container
 Utilize implicit data sharing efficiently
 Example
Object obj0;
Object obj1, obj2, obj3 = obj0
 Here data is only copied if some other guy modifies it since Object 1
will have deep copy of object ObjectData and Object 2 and Object 3
will only have shallow copies
 Avoid deep-copy, its expensive
 Pass objects around as const references
 Only use const operators and functions if possible
 Avoid bottlenecks
To learn more about Optimizing your
Qt Apps check
 Optimizing Performance in Qt Based Applications Bjørn Erik Nilsen
http://www.slideshare.net/qtbynokia/optimizingperformance-in-qtbased-applications
Thanks