Qt

Topics related to Qt:

Getting started with Qt

As official documentation stated, Qt is a cross-platform application development framework for desktop, embedded and mobile. Supported Platforms include Linux, OS X, Windows, VxWorks, QNX, Android, iOS, BlackBerry, Sailfish OS and others.

This section provides an overview of what Qt is, and why a developer might want to use it.

It should also mention any large subjects within Qt, and link out to the related topics. Since the documentation for qt is new, you may need to create initial versions of those related topics.

CMakeLists.txt for your Qt project

Qt - Dealing with Databases

  • You will need the Qt SQL plugin corresponding to the type given to QSqlDatabase::addDatabase
  • If you don't have the required SQL plugin, Qt will warn you that it can't find the requested driver
  • If you don't have the required SQL plugin you will have to compile them from the Qt source

Signals and Slots

Model/View

QTimer

QTimer can also be used to request a function to run as soon as the event loop has processed all the other pending events. To do this, use an interval of 0 ms.

// option 1: Set the interval to 0 explicitly.
QTimer *timer = new QTimer;
timer->setInterval( 0 );
timer->start();

// option 2: Passing 0 with the start call will set the interval as well.
QTimer *timer = new QTimer;
timer->start( 0 );

// option 3: use QTimer::singleShot with interval 0
QTimer::singleShot(0, [](){
    // do something
});

qmake

Threading and Concurrency

A few notes that are already mentioned in the official docs here and here:

  • If an object has a parent, it has to be in the same thread as the parent, i.e. it cannot be moved to a new thread, nor can you set a parent to an object if the parent and the object live in different threads
  • When an object is moved to a new thread, all of its children are also moved to the new thread
  • You can only push objects to a new thread. You cannot pull them to a new thread, i.e. you can only call moveToThread from the thread where the object is currently living in

Deploying Qt applications

Using Style Sheets Effectively

Qt Container Classes

Qt provides its own template container classes. They are all implicitly shared. They provide two kinds of iterators (Java style and STL style.)

Qt sequential containers include: QVector, QList, QLinkedList, QStack, QQueue.

Qt associative containers include: QMap, QMultiMap, QHash, QMultiHash, QSet.

QObject

QObject class is the base class for all Qt objects.

Implicit sharing

STL style iterators on Qt Container can have some negative side effect due to the implicit-sharing. It is advised to avoid copying a Qt container while you have iterators active on them.

QVector<int> a,b; //2 vectors
a.resize(1000); 
b = a; // b and a now point to the same memory internally

auto iter = a.begin(); //iter also points to the same memory a and b do
a[4] = 1; //a creates a new copy and points to different memory.
//Warning 1: b and iter point sill to the same even if iter was "a.begin()"

b.clear(); //delete b-memory
//Warning 2: iter only holds a pointer to the memory but does not increase ref-count. 
//           so now the memory iter points to is invalid. UB!

QGraphics

Multimedia

Qt Multimedia is a module providing handling of multimedia (audio, video) and also camera and radio functionality.

However, the supported files of QMediaPlayer depends on the platform. Indeed, on windows, QMediaPlayer uses DirectShow, on Linux, it uses GStreamer. So depending on the platform some files may work on Linux but not on Windows or the opposite.

QDialogs

The QDialog class is the base class of dialog windows. A dialog window is a top-level window mostly used for short-term tasks and brief communications with the user. QDialogs may be modal or modeless.

Note that QDialog (and any other widget that has type Qt::Dialog) uses the parent widget slightly differently from other classes in Qt. A dialog is always a top-level widget, but if it has a parent, its default location is centered on top of the parent's top-level widget (if it is not top-level itself). It will also share the parent's taskbar entry.

A modal dialog is a dialog that blocks input to other visible windows in the same application. Dialogs that are used to request a file name from the user or that are used to set application preferences are usually modal. Dialogs can be application modal (the default) or window modal.

The most common way to display a modal dialog is to call its exec() function. When the user closes the dialog, exec() will provide a useful return value.

A modeless dialog is a dialog that operates independently of other windows in the same application. Modeless dialogs are displayed using show(), which returns control to the caller immediately.

Common Pitfalls

Build QtWebEngine from source

Communication between QML and C++

Qt Resource System

About using layouts, widget parenting

From Qt layout documentation:

When you use a layout, you do not need to pass a parent when constructing the child widgets. The layout will automatically reparent the widgets (using QWidget::setParent()) so that they are children of the widget on which the layout is installed.

So do :

QGroupBox *box = new QGroupBox("Information:", widget);
layout->addWidget(box);

or do :

QGroupBox *box = new QGroupBox("Information:", nullptr);
layout->addWidget(box);

is exactly the same.

Header on QListView

Qt Network

SQL on Qt