30
Qt Everywhere: a C++ abstraction platform Gianni Valdambrini ([email protected])

Qt everywhere a c++ abstraction platform

Embed Size (px)

Citation preview

Page 1: Qt everywhere   a c++ abstraction platform

Qt Everywhere: a C++ abstraction platform

Gianni Valdambrini ([email protected])

Page 2: Qt everywhere   a c++ abstraction platform

2

C++ and portability issues Is C++ portable? Yes, theoretically...

Standard library is too tiny:

No filesystem access No network No multiprocessing/multithreading No serious unicode support ... and it's not going to change in C++0x!

Page 3: Qt everywhere   a c++ abstraction platform

3

C++ and portability issues Real-world C++ programmers have to use libraries for

everything

Some libraries are good, some are bad Some are portable, some are not Some become unmaintained after a while ... big mess!

Page 4: Qt everywhere   a c++ abstraction platform

4

C++ and portability issues Windows programmers use Win32 API

But Microsoft only improves .NET framework! Naked Win32 API is a pain to use!

Linux programmers use POSIX POSIX is mostly C-oriented It's totally useless for Windows

Mac C++ programmers are doomed Either POSIX or Carbon... Cocoa is for Objective-C only!

Page 5: Qt everywhere   a c++ abstraction platform

5

Qt to the rescue! Qt is not only a GUI toolkit: it's also a complete

portability layer!

Even for command line applications Lots of carefully designed and very mature classes for

everyday C++ programming

Prefer quick everyday usability over “abstract perfection”

Fast to learn, fast to use, compact code

Page 6: Qt everywhere   a c++ abstraction platform

6

Qt Portability Layer QtCore

Data structures, rich types, system information, reactor (event loop), multi-threading

QtNetwork

Sockets, IP, TCP, UDP, HTTP, FTP, SSL QtXml

Full DOM implementation SAX2 parser

QtWebKit

Web browser engine, used by Safari/Chrome

Page 7: Qt everywhere   a c++ abstraction platform

7

QtCore: quick overview QtCore is very “wide” but not “deep”

Many simple classes But lots of them!

Exploring all it would require a full training on its own!

We will just see some random examples, that will show the typical design of QtCore classes

Easy to use, and very powerful

Page 8: Qt everywhere   a c++ abstraction platform

8

QtCore: Implicit sharing Many Qt classes use “implicit sharing”

Each instance is actually a pointer to shared data, with a reference counter

Net result: fast pass-by-value, easier code, less performance bugs.

Fully multithreaded safe

No mutex/locks, use CPU-specific atomic increments/decrements.

Page 9: Qt everywhere   a c++ abstraction platform

9

QtCore: Implicit sharing

Returning by value offer more readable API:

QList<QObject> c = obj->children(); Chainability: obj->children().count(); Can also nest containers with no overhead

With STL containers, it would have been:

list<QObject> c; obj->children(c); Using an argument as return value is bad for

readability!

Page 10: Qt everywhere   a c++ abstraction platform

10

Text processing Qt offers many good primitives for text processing. QFile: line-by-line processing QRegExp: Perl regular expressions (many methods

accept it, even findChildren()!) QString: many powerful methods (inspired by Perl)

Page 11: Qt everywhere   a c++ abstraction platform

11

QtCore: strings (1/3) QString: unicode strings representation

Implicit sharing policy: pass by value!

Well-suited for text, don't use for binary data

QByteArray is better for 8-bit binary data

Contains loads of functionality for any kind of text processing

Page 12: Qt everywhere   a c++ abstraction platform

12

QtCore: strings (2/3) Basic: constructor, resize(), [] / .at(), .size(), .resize(),

.fill()

Modification: .append(), .prepend(), .insert(), .replace() (RX), .remove() (RX)

Whitespaces: .trimmed(), simplified()

Grouping: .split() (RX), .join()

Searching: .indexOf(), .lastIndexOf(), .find() (RX), .{starts|ends}With(), .contains() (RX), .count() (RX)

Comparison: <, <=, ==, >=, >, ::localeAwareCompare()

Page 13: Qt everywhere   a c++ abstraction platform

13

QtCore: strings (3/3) Conversion to: .toUpper(), .toLower(), toInt(),

.toDouble(), .toAscii(), .toLatin1(), .toUtf8(),

.toLocal8Bit(), toUcs4()

Conversion from: like above, ::number(n,base)

Formatting: .sprintf() (bad), .arg() (good)

Use %number formatting instead of %type (like printf)

Fully type-safe More translator-friendly (can safely-reorder)

... and much more!

Page 14: Qt everywhere   a c++ abstraction platform

14

QtCore: date/time support (1/3)

QDate: one instance is one specific calendar day

Formatting: .{to|from}String() (ISO or locale) Weeks: .weekNumber(), .dayOfWeek() Jumping: .addDays/Months/Years() Texts: ::{long|short}{Day|Month}Name() Validation: ::isLeapYear(), ::isValid() Ordering: <, <=, ==, >=, >, .daysTo() Factory: ::currentDate()

Page 15: Qt everywhere   a c++ abstraction platform

15

QtCore: date/time support (2/3)

QTime: one instance is one specific time (instant)

Formatting: .{to|from}String() (ISO or locale) Jumping: .addSecs(), .addMSecs() Validation: ::isValid() Measuring: .start(), .restart(), .elapsed() Ordering: <, <=, >=, >, .secsTo(), .msecsTo() Factory: ::currentTime()

Page 16: Qt everywhere   a c++ abstraction platform

16

QtCore: date/time support (3/3)

QDateTime: an instance is one specific calendar time at a specific time (instant)

Combination of the previous two, merging all the functionalities!

Time formats: Localtime/UTC

Page 17: Qt everywhere   a c++ abstraction platform

17

STL containers STL containers are good because:

They are standard C++ They are ready-to-use and very efficient The generic iterator concept is powerful

STL containers are bad because:

They are verbose to use (eg: iterating) They are incomplete

Hash table anyone? They bloat the executable code due to template

expansion

Page 18: Qt everywhere   a c++ abstraction platform

18

Qt Containers Designed “the Qt way”

Easy to use, no complex stuff Implicitly shared

You can pass by value without impacting performance (more readable code!)

Minimal inline expansion

Generate light executables for embedded code

Page 19: Qt everywhere   a c++ abstraction platform

19

Qt Containers QList<>, QLinkedList<>, QVector<>

QList<> is an array of pointers Random access Best for most purpose (very fast operations)

QLinkedList<> is a true linked list of pointers O(1) insertion in the middle, no random access

QVector<> is an array of objects Occupy contiguous space in memory Slow insertions, but cache friendly

Page 20: Qt everywhere   a c++ abstraction platform

20

Qt Containers QVector<> is an array of objects

Occupy contiguous space in memory Slow insertions, but cache friendly

QList<> is an array of pointers

Random access Best for most purpose (very fast operations)

QSet<>

Fast set of objects (no repetition) Implemented through hash-table (much faster

than std::set!) not ordered→

Page 21: Qt everywhere   a c++ abstraction platform

21

Qt Containers QHash<>, QMap<>

Associative arrays (map values to keys) QHash<> relies on qHash() and operator==(), QMap

on operator<() Hashtables are O(1) for lookups, maps are O(logn) Iteration: hashtables have random order, maps have

fixed order

Page 22: Qt everywhere   a c++ abstraction platform

Qt / XML parsers

Stream based

QXmlReader

SAX2 QXmlStreamReader

Qt specific

DOM

QtDom*

W3C

Page 23: Qt everywhere   a c++ abstraction platform

QXmlReader

SAX2 only http://www.saxproject.org

not validating, namespace support

SAX2 parsing is callback based

for each sax-event a different function is called the parser has no state

Page 24: Qt everywhere   a c++ abstraction platform

QDom*

in memory representation of the XML document

follow the W3C recommendations

useful to manipulate the XML document

not validating, namespace support

Page 25: Qt everywhere   a c++ abstraction platform

QtNetwork

Qt module to make networking programs

QtGui not required high-level classes

for http/ftp clients low-level classes

direct access to the underling socket event driven (= high performance)

SSL support

Page 26: Qt everywhere   a c++ abstraction platform

QtNetwork

high level classes

QNetworkAccessManager

QFtp

low level classes

QTcpServer

QTcpSocket

QUdpServer

QUdpSocket

QSsl*

Page 27: Qt everywhere   a c++ abstraction platform

QtNetwork

QMetworkAccessManager

event driven the event loop must be running → QNetworkRequest

full support for setting http headers QNetworkReply

access to the reply headers signals to track the downloading process

Page 28: Qt everywhere   a c++ abstraction platform

QtNetwork

QNetworkDiskCache

simple, disk based, cache QAbstractNetworkCache for a custom cache

QnetworkProxy

route network connections through a proxy SOCK5, HTTP, HTTP Caching, FTP Caching

transparent to the networking code application wide / per socket

anonymous or username/password

Page 29: Qt everywhere   a c++ abstraction platform

29

Any Questions?

??

Page 30: Qt everywhere   a c++ abstraction platform

GRAZIE !GRAZIE !

Contatti

Mail: [email protected]

Phone: +39-055-3984627

Fax: +39 178 6003614

http://www.develer.com

Develer S.r.l.Via Mugellese 1/A

50013 Campi BisenzioFirenze - Italia