QtApplication::exec -> starts main event loop defined in QtCoreApplication
Qt framework is divided into modules. Each module is a library. QtCore, QtGUI and QtWidgets are used by default.
QtCoreApplication designed for inheritance (to add some global data to the application, or override some functions). This way developer can avoid using singleton anti-pattern.
when using multiple inheritance QObject must be the first one. Developer must avoid multiple inheritance resulting in multiple iheritance from QObject or any other Qt base class.
Qt extends C++ with meta info (aka Class in Java). This meta is used by MOC (MetaObjectCompiler). MOC “replaces” Qt macro, e.g. Q_OBJECT with required code in a separate file.
resources are defined in special XML file (with .qrc extension) and can be accessed in Runtime using the following path: “:/folder/filename.ext”, e.g. picture->setPixmap(QPixmap(“:/images/open.png”))
qInstallMessageHandler(func) attaches handler for QMessages, aka gDebug etc. Can be used to redirect messages to file.
QtGlobal has a number of usefull methods and macros
QLibraryInfo contains info about current Qt setup (path to plugins, library version etc)
QApplication::setPalette() defines palette for the whole application. Good practice is to use 3-7 colors in a asingle application
by default Qt supports double buffering for painting. QWidget::setAttribute(Qt::WA_PaintOnScreen) disables this.
Build/Run
on Windows add console option to CONFIG variable in the .pro file to see console output
QObject
defines signal/slot; timer; hierarchy (simplifies memory managmet: parent object destroys its children); signals filter; meta info (aka Class in Java); qobject_cast()
to inherit from QObject developer must add Q_OBJECT marco just after class declaration (no semi-colon!!!)
Q_PROPERTY macro defines object property visible to QScript and UIDesigner. At minumum must provide name and read function: Q_PROPERTY(bool myProp READ getMyProp)
parent of the object is defined in the constructor (ptr-to-QObject) or using setParent(). parent() and children() returns info about parent and children correspondignly.
setObjectName() and objectName() can be very useful for debugging
child of the object can be found using findChild<QObject>(const char), where argument is the name of the object or a regex
rule of thumb – create all objects on the heap, via new. Only objects without children may be created on the stack (short live objects)
dumpObjectInfo() and dumpObjectTree() prints corresponding info in stdout
QObject::metaObject() aka this.getClass() in Java
QObject::inherits() aka instanceof
QObject is not copyable nor assignable. These are defined as private.
Signals/Slots
to define signal put a method under signals: section of the class declaration (similar to public, private etc)
signal does not have definition – only declaration. MOC generates all required code.
signal always returns void
to send a signal use emit keyword
slots can be private, protected and public, e.g. public slots:
slots do not have arguments with default values
avoid virtual slots (barzo slow)
slot method provides acces to the sender via sender(). sender() returns ptr to QObject that has emitted the signal
signal/slots are connected via QObject::connect method
conncetion can be of three types: Direct, Queued and Auto. Direct – aka method call; Queued – signal is transformed into event and queued; Auto – if in the same thread -> Direct, otherwise Queued.
connections of the object are removed automatically when object is removed but can also be removed via QObject::disconnect method
QSignalMapper maps signal onto some function. It can define parameters, e.g. QString: mapper->setMapping(QObject, “Value”)
Events
events are low level signals e.g. QMouseEvent provides info about mouse location and/or which button was pressed
each event is handled by a single method (virtual protected) from which developer can emit a signal
QEvent is the base class for all events defined in Qt
if QEvent::ignore() is set event handling will be delegated to its succesors, if any
events are handled after QApplication::exec() is called
by default mouseMoveEvent() is called only if mouse is moving when one of its button is pressed. QWidget::setMouseTracking(true) enables mouse movement events without buttons
custom events can be sent using QCoreApplication::sendEvent() or QCoreApplication::postEvent()
custom events can be recieved with QObject::event() or QObject::customEvent()
it is possible to override event method, but this can blow up the code base. Better approach is to override specific event handler function and change event only in case specific method does not exist
QCoreApplication::processsEvents() ensures that all queued up till now events will be processed. It is a good practice to insert this after heavy operations so that application does not freeze totally
QCoreApplication::processsEvents() ensures that all queued up till now events will be processed. It is a good practice to insert this after heavy operations so that application does not freeze totally
to define event filter inherit from QObject and override eventFilter() so that if it returns true event won’t further processed, otherwise event will be passed to the destination object
to attach filter object use QObject::installEventFilter(filter). This way filter functionality can be easily shared
QCoreApplication::installFilter() installs filter for evey widget in the app. May affect event delivery perfomance
QCoreApplication::notify() defines the way how events are delivered. Ovveride this one for full control over the process
QXXIterator have same semantics as Java Iterators, aka next(), hasNext(). It is an object (not pointer) that points to siblings of the element not the element.
QXXMutableIterator allow changes to the underlying object: remove(), insert() and setValue()
Qt creates a copy of a container when enetering foreach
QByteArray can be compressed and uncompressed uaing global functions qCompress and qUncompress(). QByteArray::toBase64, QByteArray::fromBase64
QBitArray aka BitSet
QList::at() returns const ref to an element
prefer simple value when specifying associative container’s size
operator[] creates a new element if it does not exists, i.e. map["MyKey"] returns newly created value, if it does not exists
QMapXX sorts by key while QHashXX uses hash-table
to be eligible for QHash object must have operator== and qHash() defined
QHash::squeeze reduces hash-table size
QtAlgorithms
qEqual – compares two containers. Elements must have operator==
QString
contains text data in Unicode (2 bytes per symbol)
str.setNum(3.14) is the same as QString::number(3.14) and is the smae as QTextStream(&str) << 3.14
when created without value (e.g. on the stack) QString points to shared_null
QVariant
contains value of an arbitrary type. QVariant::type returns actual type’s id (int); QVariant::typeName returns actual type as string
to extract value use toXX() or value(). This creates a copy of the value.
QWidget::setAutoFillBackground(true) makes widget visible, e.g. fill widget with blue color
QStackedWidget shows one widget at a time
widgets can be organized using QLayout. setSpacing() and setMargin() are used for fine tuning
QLayout assigns root widget (defined by setLayout()) as a parent to newly added widgets
tab order be default by the order widgets were added to the layout
QSplitter is a layout with a movable border between widgets
QLabel can actually show graphics (setPixmap()) or even multimedia (setMovie()). Hold on a sec – it renders HTML!!!
QLabel can be grouped with any other widget using setBuddy()
Using & defines shortcut, e.g. N&ame defines shortcut — <Alt>+a
To define sort order in QListWidget its items must inherit from QListWidgetItem (QTreeWidgetItem for trre like lists) and override operator<()
QWidget::palette() returns QPalette which defines colors for three widget states: Active, inactive and Disabled
QCursor
QGuiApplication::setOverrideCursor(Qt::cursor) overrides cursor for the whole application, e.g. loading cursor when application loads something; QGuiApplication::restoreOverrideCursor() – restores default behavior
MV, aka Interview
QAbstractProxyModel is a convenient abstraction for sortinf, filtering and other manipulations with model
to define selection model, aka data binding in webix, use QAbstarctItemView::selectionModel()
to check whether requested index is valid (QModelIndex index = pModel->index(2, 5, QModelIndex());) use QModelIndex::isValid(). Index may be invalid in case these is no data at the position in the model specified by the index.
each data element in the model may have its won role, aka tooltip, decoration etc
Arthur (drawing engine)
Qt provides double buffering out-of-the-box transparantely for a developer
QPainter, QPaintEngine, QPaintDevice are base classes in Arthur.
QPainter is used to draw
QPainter::drawText(x, y, str) draws str text. To define font used for drawing text one uses QPainter::setFont()
QPaintDevice is an abstraction i.e. something to draw on, aka QWidget, QImage etc
in general drawing is performed in QPaintEvent handler. For instance if user movesa window and it overlaps another window, overlapped window will exeucte QWidget::paintEvet()
QPicture provides a context with history. This history can be saved into a file and then reused.
Qt provides 4 predefined graphic effects: blur; colorization; drop shadow and opacity
to implement custom graphics effect one inherits from QGraphicsEffect and overrides draw()
QImage is non-context-independent class for drawing, aka all drawing happens in memory
to enanable Qt to render specified image format a corresponding dll (so) must supplied with the application. These files must reside in /imageformats folder
::load() can be used to load image at any time (not just at construction)
QPixmap is context-dependent class and must be used for drawing on display etc