Qt5/Qt6 Compatibility Guide¶
This document introduces the compatibility macros and strategies used in the DAWidgets project to handle API differences between Qt5 and Qt6.
Core Principle¶
DAWidgets is a Qt5/Qt6 dual-compatible library — this is a hard requirement. All version differences are handled through compatibility macros in DAQt5Qt6Compat.h and #if QT_VERSION guards.
Common API Differences and Compatibility Macros¶
1. QMouseEvent / QTouchEvent pos() Method¶
In Qt6, pos() is deprecated, replaced by position().toPoint():
1 2 3 4 5 6 | |
Related macros:
| Macro | Qt5 Expansion | Qt6 Expansion |
|---|---|---|
Qt5Qt6Compat_QXXEvent_Pos(ptr) |
ptr->pos() |
ptr->position().toPoint() |
Qt5Qt6Compat_QXXEvent_x(ptr) |
ptr->pos().x() |
ptr->position().x() |
Qt5Qt6Compat_QXXEvent_y(ptr) |
ptr->pos().y() |
ptr->position().y() |
2. QFontMetrics width() Method¶
From Qt 5.11+, width() is deprecated, replaced by horizontalAdvance():
1 2 3 4 5 | |
| Macro | Qt < 5.12 Expansion | Qt >= 5.12 Expansion |
|---|---|---|
Qt5Qt6Compat_fontMetrics_width(fm, str) |
fm.width(str) |
fm.horizontalAdvance(str) |
3. QButtonGroup Signal Changes¶
In Qt6, buttonClicked(int) is renamed to idClicked(int):
1 2 3 4 5 6 | |
4. QString::split SkipEmptyParts¶
1 2 3 4 5 6 | |
5. QApplication::globalStrut()¶
Qt6 removed globalStrut(), requiring conditional compilation to work around.
Include Strategy¶
Include Strategy
DAQt5Qt6Compat.h is included only in .cpp files on an as-needed basis by default, avoiding propagation through public headers. This ensures that when data-workbench integrates DAWidgets, it does not passively inherit new compatibility macros.
1 2 3 4 5 6 7 8 | |
Maintenance Contract¶
Maintenance Contract
The macro definitions in DAQt5Qt6Compat.h must be verbatim identical to the corresponding macros in the data-workbench project's DAGlobals.h. Both sides are guarded by #ifndef. You must not independently modify the macro definitions in this file — if changes are needed, you must synchronously update data-workbench's DAGlobals.h.
Checklist¶
When adding new Qt version difference handling:
- Add the compatibility macro in
DAQt5Qt6Compat.h(with#ifndefguard) - Use
#if QT_VERSION < QT_VERSION_CHECK(...)for version checks - Include the compatibility header only in .cpp files
- Synchronously update data-workbench's
DAGlobals.h - Verify compilation under both Qt5 and Qt6