a brief introduction to the qt application framework
DESCRIPTION
This is a brief introduction to Qt, the cross-platform, open-source, C++ application framework.TRANSCRIPT
![Page 1: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/1.jpg)
August 8, 2011
![Page 2: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/2.jpg)
Introduction
Container Classes and Strings
Widgets and GUIs
Resources
PROGRAMMING WITH QT
![Page 3: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/3.jpg)
Cross-platform C++ application development framework
Effectively adds a few new C++ keywords (signal, slot, emit, etc)
Provides facilities for:◦ GUIs◦ Internationalization◦ XML serialization◦ Media◦ More
![Page 4: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/4.jpg)
“Write once, compile anywhere”
GUIs look native (or pretty close to it) with just a recompile
Commercial, GPL and LGPL licences
![Page 5: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/5.jpg)
European Space Agency
Google Earth
K Desktop Environment (KDE)
Adobe Photoshop Album
VLC Player
Autodesk Maya
…
![Page 6: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/6.jpg)
![Page 7: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/7.jpg)
C++ class library for writing GUIs, database access, XML parsing, etc
![Page 8: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/8.jpg)
Development tools (you generally don‟t have to use them if you prefer your own)
![Page 9: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/9.jpg)
qmake enables you to write one project file (with the extension “.pro”) and generate platform-
specific project files from that as needed
![Page 10: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/10.jpg)
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel label("Hello world!");
label.show();
return a.exec();
}
![Page 11: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/11.jpg)
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel label("Hello world!");
label.show();
return a.exec();
}
Headers named after the class they define
![Page 12: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/12.jpg)
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel label("Hello world!");
label.show();
return a.exec();
}
All Qt apps have a QApplication or
QCoreApplicationinstance
![Page 13: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/13.jpg)
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel label("Hello world!");
label.show();
return a.exec();
}
The label widget becomes our window
because it has no parent
![Page 14: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/14.jpg)
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel label("Hello world!");
label.show();
return a.exec();
}
QApplication::exec() runs the message
loop and exits when the user closes the
window
![Page 15: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/15.jpg)
Introduction
Container Classes and Strings
Widgets and GUIs
Resources
PROGRAMMING WITH QT
![Page 16: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/16.jpg)
QList
QMap
QStack
QQueue
QSet
QVector
Others…
![Page 17: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/17.jpg)
QList
QMap
QStack
QQueue
QSet
QVector
Others…
QList is the most frequently used container, and can be used for storing an
ordered list of items
![Page 18: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/18.jpg)
QList
QMap
QStack
QQueue
QSet
QVector
Others…
QMap is frequently used when one needs to store key-value pairs and perform fast lookups
on the key value
![Page 19: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/19.jpg)
Grow as needed
Iteration using various methods
for (int i = 0; i < list.size(); i++)
qDebug() << list[i];
QList<QString> list;
list << “Item1” << “Item2”;
list.append(“Item3”);
list.push_back(“Item4”);
![Page 20: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/20.jpg)
Grow as needed
Iteration using various methods
for (int i = 0; i < list.size(); i++)
qDebug() << list[i];
QList<QString> list;
list << “Item1” << “Item2”;
list.append(“Item3”);
list.push_back(“Item4”);
Multiple methods for appending (<<, append, push_back) are all equivalent.
![Page 21: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/21.jpg)
Implicitly shared
Easy constructs for iteration: foreach!!!
QList<QString> list;
foreach (const QString &str, list)
qDebug() << str;
QList<QString> list1;
list1 << “Item1” << “Item2”;
QList<QString> list2 = list1;
![Page 22: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/22.jpg)
Implicitly shared
Easy constructs for iteration: foreach!!!
QList<QString> list;
foreach (const QString &str, list)
qDebug() << str;
QList<QString> list1;
list1 << “Item1” << “Item2”;
QList<QString> list2 = list1;
Only a quick, constant-time, shallow copy of list1 is made here. If list1 or list2 is
modified later, a deep copy will be made at that time. This is called Implicit Sharing and is a core
Qt concept.
![Page 23: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/23.jpg)
QString!
Built-in Unicode support
Useful methods (trimmed(), split())
Cheap copying (implicit sharing)
Believe it or not, they can actually contain embedded „\0‟ characters…
![Page 24: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/24.jpg)
Concatenation and formatting
Translation
QString first = “James”;
QString last = “Kirk”;
QString fullName = first + “ “ + last;
QString fullName2 =
QString(“%1 %2”).arg(first).arg(last);
QString translatedSend = tr(“Send”);
![Page 25: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/25.jpg)
Concatenation and formatting
Translation
QString first = “James”;
QString last = “Kirk”;
QString fullName = first + “ “ + last;
QString fullName2 =
QString(“%1 %2”).arg(first).arg(last);
QString translatedHello = tr(“Hello”);
Both fullName and fullName2 end up
the same: “James Kirk”
![Page 26: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/26.jpg)
Concatenation and formatting
Translation
QString first = “James”;
QString last = “Kirk”;
QString fullName = first + “ “ + last;
QString fullName2 =
QString(“%1 %2”).arg(first).arg(last);
QString translatedHello = tr(“Hello”);
If a translation for “Hello” is available at runtime, tr(“Hello”) will return that translated string instead of “Hello”.
![Page 27: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/27.jpg)
Splitting/joining on a delimiter character
Removing leading or trailing whitespace
QString numbers = "1,2,54,23,7";
QStringList nums = numbers.split(',');
int sum = 0;
foreach (QString num, numbersList)
sum += num.toInt();
QString name = “ William Shatner\n“;
QString trimmedName = name.trimmed();
![Page 28: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/28.jpg)
Splitting/joining on a delimiter character
Removing leading or trailing whitespace
QString numbers = "1,2,54,23,7";
QStringList nums = numbers.split(',');
int sum = 0;
foreach (QString num, numbersList)
sum += num.toInt();
QString name = “ William Shatner\n“;
QString trimmedName = name.trimmed();
Also available: QString::toLong(), QString::toDouble(), QString::toLower(), etc
![Page 29: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/29.jpg)
Splitting/joining on a delimiter character
Removing leading or trailing whitespace
QString numbers = "1,2,54,23,7";
QStringList nums = numbers.split(',');
int sum = 0;
foreach (QString num, numbersList)
sum += num.toInt();
QString name = “ William Shatner\n“;
QString trimmedName = name.trimmed();
Afterwards, trimmedName equals
“William Shatner”
![Page 30: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/30.jpg)
Introduction
Container Classes and Strings
Widgets and GUIs
Resources
PROGRAMMING WITH QT
![Page 31: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/31.jpg)
Most objects inherit from QObject:
◦ QWidget
◦ QThread
◦ Etc..
Together with the mocprecompiler, enables many Qt features…
![Page 32: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/32.jpg)
Signals and slots
Memory management (parents kill their children)
Properties
Introspection (QObject::className(), QObject::inherits())
BUT:
QObjects cannot be copied (they have “identity”)
![Page 33: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/33.jpg)
General-purpose way for Qt objects to notify one-another when something changes.
Simple Observer pattern:
QPushButton *button = new
QPushButton(“About", this);
QObject::connect(
button, SIGNAL(clicked()),
qApp, SLOT(aboutQt()));
![Page 34: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/34.jpg)
General-purpose way for Qt objects to notify one-another when something changes.
Simple Observer pattern:
QPushButton *button = new
QPushButton(“About", this);
QObject::connect(
button, SIGNAL(clicked()),
qApp, SLOT(aboutQt()));
When the button emits the “clicked()” signal, the global application‟s
“aboutQt()” slot will be executed.
![Page 35: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/35.jpg)
Parents destroy their children… A good thing!?
No memory leak! aboutDialog‟s destructor deletes label.
void showAbout()
{
QDialog aboutDialog(NULL);
QLabel* label = new QLabel(
"About this..", &aboutDialog);
aboutDialog.exec();
}
![Page 36: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/36.jpg)
Parents destroy their children… A good thing!?
No memory leak! aboutDialog‟s destructor deletes label.
void showAbout()
{
QDialog aboutDialog(NULL);
QLabel* label = new QLabel(
"About this..", &aboutDialog);
aboutDialog.exec();
}
label is freed when its parent, aboutDialog, goes
out of scope.
![Page 37: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/37.jpg)
So what happens if the child is on the stack?
void showAbout()
{
QDialog* about = new QDialog(NULL);
QLabel label(
"About this..", aboutDialog);
about->exec();
delete about;
}
![Page 38: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/38.jpg)
So what happens if the child is on the stack?
◦ Crash!! Qt tries to free the stack-allocated child!
void showAbout()
{
QDialog* about = new QDialog(NULL);
QLabel label(
"About this..", aboutDialog);
about->exec();
delete about; // CRASH!
}about‟s destructor tries to free label, but label is on
the stack, not the heap!
![Page 39: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/39.jpg)
Introduction
Container Classes and Strings
Widgets and GUIs
Resources
PROGRAMMING WITH QT
![Page 40: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/40.jpg)
Available as a free PDF from author at:
http://www.qtrac.eu/marksummerfield.html
![Page 41: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/41.jpg)
http://www.ics.com/learning/icsnetwork/
![Page 42: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/42.jpg)
http://doc.qt.nokia.com
![Page 43: A Brief Introduction to the Qt Application Framework](https://reader031.vdocuments.net/reader031/viewer/2022020116/555ad827d8b42a024a8b4d84/html5/thumbnails/43.jpg)