Skip to content

Developer Notes for Qt Code

Hennadii Stepanov edited this page Feb 25, 2021 · 13 revisions

Contents

  1. Backward Compatibility
  2. QObject Subclassing Style
  3. Debugging Tips

Backward Compatibility

The source code must be compatible with the minimum required Qt version which is set in the configure.ac:

BITCOIN_QT_CONFIGURE([5.5.1])

If an optional feature requires Qt higher version, or a feature was replaced by another one, use QT_VERSION and QT_VERSION_CHECK macros:

#include <QDate>
#include <QDateTime>
#include <QtGlobal>  // For QT_VERSION and QT_VERSION_CHECK macros.

QDateTime StartOfDay(const QDate& date)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    return date.startOfDay();  // QDate::startOfDay was introduced in Qt 5.14.0.
#else
    return {date};
#endif
}

Do not compare QT_VERSION directly to Qt version of the form 0xMMNNPP (MM = major, NN = minor, PP = patch) as such an approach is less readable and more error prone.

Every time the minimum required Qt version is bumped, grep all of the QT_VERSION instances and adjust/remove them accordingly.

QObject Subclassing Style

When sublassing the QObject class follow the pattern below:

#include <QObject>

class MyWidget : public QObject
{
    Q_OBJECT

public:
    // Public members.

public Q_SLOTS:
    // Public slots.

Q_SIGNALS:
    // Signals (inherently public).

private:
    // Private members.

private Q_SLOTS:
    // Private slots.
};

Note that Q_SIGNALS and Q_SLOTS macros are used instead of the signals and slots keywords of the Qt moc (Meta-Object Compiler). It prevents potential conflicts with a 3rd party signal/slot mechanism.

Debugging Tips

For debugging, including signal to slot connection issues, one could use the QT_FATAL_WARNINGS environment variable:

$ QT_FATAL_WARNINGS=1 src/qt/bitcoin-qt -printtoconsole -debug=qt

This tip could be combined with a debugger.


More notes come soon...