Your SlideShare is downloading. ×
0
August 8, 2011
PROGRAMMING WITH QT   Introduction   Container Classes and Strings   Widgets and GUIs   Resources
   Cross-platform C++ application development    framework   Effectively adds a few new C++ keywords (signal,    slot, e...
   “Write once, compile anywhere”   GUIs look native (or pretty close to it) with just a    recompile   Commercial, GPL...
   European Space Agency   Google Earth   K Desktop Environment (KDE)   Adobe Photoshop Album   VLC Player   Autodes...
C++ class library for writing GUIs,database access, XML parsing, etc
Development tools (you generally don‟thave to use them if you prefer your own)
qmake enables you to write one project file (with the extension “.pro”) and generate platform-   specific project files fr...
#include <QApplication>#include <QLabel>int main(int argc, char *argv[]){    QApplication a(argc, argv);    QLabel label("...
#include <QApplication>    Headers named after#include <QLabel>          the class they defineint main(int argc, char *arg...
#include <QApplication>    #include <QLabel>All Qt apps have a QApplication or       int main(intQCoreApplication         ...
#include <QApplication>#include <QLabel>int main(int argc, char *argv[])                             The label widget{    ...
#include <QApplication>#include <QLabel>int main(int argc, char *argv[]){    QApplication a(argc, argv);    QLabel label("...
PROGRAMMING WITH QT   Introduction   Container Classes and Strings   Widgets and GUIs   Resources
   QList   QMap   QStack   QQueue   QSet   QVector   Others…
QList is the most frequently used   QList     container, and can be used for storing an                         ordered l...
   QList                QMap is frequently used when one needs to   QMap      store key-value pairs and perform fast loo...
   Grow as needed    QList<QString> list;    list << “Item1” << “Item2”;    list.append(“Item3”);    list.push_back(“Item...
   Grow as needed     Multiple methods for appending (<<,                       append, push_back) are all equivalent.   ...
   Implicitly shared    QList<QString> list1;    list1 << “Item1” << “Item2”;    QList<QString> list2 = list1;   Easy co...
   Implicitly shared    QList<QString> list1;    list1 << “Item1” << “Item2”;    QList<QString> list2 = list1;   Easy co...
   QString!   Built-in Unicode support   Useful methods (trimmed(),    split())   Cheap copying (implicit sharing)   ...
   Concatenation and formatting    QString first = “James”;    QString last = “Kirk”;    QString fullName = first + “ “ +...
Both fullName and fullName2 end up                                the same: “James Kirk”   Concatenation and formatting  ...
   Concatenation and formatting    QString first = “James”;    QString last = “Kirk”;                        If a transla...
   Splitting/joining on a delimiter character    QString numbers = "1,2,54,23,7";    QStringList nums = numbers.split(,);...
   Splitting/joining on a delimiter character                            Also available: QString::toLong(),              ...
   Splitting/joining on a delimiter character    QString numbers = "1,2,54,23,7";    QStringList nums = numbers.split(,);...
PROGRAMMING WITH QT   Introduction   Container Classes and Strings   Widgets and GUIs   Resources
   Most objects inherit from    QObject:    ◦ QWidget    ◦ QThread    ◦ Etc..   Together with the moc    precompiler, en...
   Signals and slots   Memory management (parents kill their children)   Properties   Introspection (QObject::classNam...
   General-purpose way for Qt objects to notify    one-another when something changes.   Simple Observer pattern:QPushBu...
   General-purpose way for Qt objects to notify    one-another when something changes.   Simple Observer pattern:       ...
   Parents destroy their children… A good thing!?void showAbout(){     QDialog aboutDialog(NULL);     QLabel* label = new...
   Parents destroy their children… A good thing!?void showAbout(){     QDialog aboutDialog(NULL);     QLabel* label = new...
   So what happens if the child is on the stack?void showAbout(){     QDialog* about = new QDialog(NULL);     QLabel labe...
   So what happens if the child is on the stack?    ◦ Crash!! Qt tries to free the stack-allocated child!void showAbout()...
PROGRAMMING WITH QT   Introduction   Container Classes and Strings   Widgets and GUIs   Resources
Available as a free PDF from author at:http://www.qtrac.eu/marksummerfield.html
http://www.ics.com/learning/icsnetwork/
http://doc.qt.nokia.com
A Brief Introduction to the Qt Application Framework
A Brief Introduction to the Qt Application Framework
Upcoming SlideShare
Loading in...5
×

A Brief Introduction to the Qt Application Framework

6,482

Published on

This is a brief introduction to Qt, the cross-platform, open-source, C++ application framework.

Published in: Technology, Education
1 Comment
7 Likes
Statistics
Notes
No Downloads
Views
Total Views
6,482
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
331
Comments
1
Likes
7
Embeds 0
No embeds

No notes for slide

Transcript of "A Brief Introduction to the Qt Application Framework"

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

    Clipping is a handy way to collect important slides you want to go back to later.

×