Qt6 has been released for some time. More and more people are asking whether the previous code can be added to the support of Qt6, including the open source project QWidgetDemo (more than 2.6K STAR one year). Recently, in the busy schedule, ALL projects have been compatible with Qt6 upgrade. Since then, all projects from Qt4 compatibility to Qt6 took more than a month, with a total of more than 10,000 lines of code updated and adjusted. Although update the workload is not very big, but still pass the skylight, after all, the number is too much, the one of dozens of projects light work custom controls over 188 child project, in order to facilitate their also share in order to summarize the experience, specially compiled from Qt4 compatible to Qt6 experience points, only partially, the late continuously updated, You are also welcome to add comments.

Intuitive to summarize

  1. Increased a lot of wheels, at the same time the original module split is also more detailed, estimated to facilitate the expansion of management.
  2. Removing things that are overly encapsulated (such as multiple functions for the same function) ensures that only one function performs that function.
  3. Some of the QT5-compatible methods have been scrapped and the corresponding new functions in Qt5 must be used.
  4. Following the pace of The Times, added many new features to meet the growing needs of customers.
  5. Some modules and types and processing of the revolutionary rewrite, running efficiency improved a lot.
  6. There are changes in parameter types, such as long * to qintptr *, which are more suitable for subsequent extensions and compatibility with different 32-bit and 64-bit systems.
  7. Source double data type all changed into Qreal, and Qt internal data type highly consistent and unified.
  8. I only tested the QWidget section, but I didn’t test the Quick section, which is likely to get more updates.
  9. It is strongly recommended not to use the version between Qt6.0 and Qt6.2 for the time being. Some modules are still missing, and relatively speaking, there are many bugs. It is recommended to start the formal migration with version 6.2.

experience

  1. Universal solution: Install version 5.15, locate the function reporting the error, switch to the source dock file, and see the corresponding prompt QT_DEPRECATED_X(“Use sizeInBytes”) and the new function. Some functions are newly added from Qt5.7, 5.9, 5.10, etc. Your project may still use the METHOD of Qt4, but Qt6 is compatible with these old methods, to Qt6 will completely need to use the new method.

  2. Qt6 splits the core class into core5Compat, so you need to add the corresponding module in Pro and introduce the corresponding header file in the code.

// Pro file import module
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat

// The header file is introduced into the code
#if(QT_VERSION > = QT_VERSION_CHECK (0, 5))
#include <QtWidgets>
#endif
#if(QT_VERSION > = QT_VERSION_CHECK (0, 6))
#include <QtCore5Compat>
#endif
Copy the code
  1. By default, Qt6 enabled high score screen support, the interface will become very large, even the font is empty, many people will not be used to, because this mode if the program does not use devicePixelRatio for many coordinate calculation, 100% will appear strange problems, because the coordinates are not accurate. To disable this effect you can set the zoom factor of the high-resolution screen.
#if(QT_VERSION > = QT_VERSION_CHECK (0, 6))
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Floor);
#endif
Copy the code
  1. QRandomGenerator QRandomGenerator QRandomGenerator QRandomGenerator QRandomGenerator QRandomGenerator QRandomGenerator QRandomGenerator There are a lot of similar packages like qSin packages of sine.

  2. The light of QColor is changed to lighter, and the dark is changed to darker. In fact, the two methods lighter and darker have been used before.

  3. FM. Width in QFontMetricsF is changed to FM. HorizontalAdvance, starting with 5.11.

  4. Foreground = WindowText, Background = Window, Foreground = WindowText, Background = Window, Foreground = WindowText, Background = Window, It used to be. Similarly, setextColor is setForeground.

  5. QWheelEvent changes delta() to angleDelta().y() and pos() to position().

  6. The SVG module is split into svgWidgets. If you use this module, add QT += svgWidgets to pro.

  7. Margin () returns contentsMargins().left(). Margin () returns contentsMargins().left(). The default values are the same. Similarly, setMargin has been removed, using setContentsMargins.

  8. QChar C = (QChar)0xf105 Conversion from ‘int’ to ‘QChar’ is ambiguous.

  9. Some functions such as qSort call back to c++ STD ::sort.

#if(QT_VERSION > = QT_VERSION_CHECK (0, 6))
    std::sort(ipv4s.begin(), ipv4s.end());
#else
    qSort(ipv4s);
#endif
Copy the code
  1. Change Qt::WA_NoBackground to Qt::WA_OpaquePaintEvent.

  2. The QMatrix class has been abandoned and replaced with QTransform. The function functions are basically the same. QTransform class has been in Qt4.

  3. The QTime timer has been removed and needs to be changed to QElapsedTimer. The QElapsedTimer class is always present in Qt4.

  4. QApplication: : desktop () abandoned, replaced by QApplication: : primaryScreen ().

#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
#include "qscreen.h"
#define deskGeometry qApp->primaryScreen()->geometry()
#define deskGeometry2 qApp->primaryScreen()->availableGeometry()
#else
#include "qdesktopwidget.h"
#define deskGeometry qApp->desktop()->geometry()
#define deskGeometry2 qApp->desktop()->availableGeometry()
#endif
Copy the code
  1. Getting the current screen index and size needs to be handled separately.
// Get the current screen index
int QUIHelper::getScreenIndex(a)
{
    // Multiple screens need to be processed
    int screenIndex = 0;
#if(QT_VERSION > = QT_VERSION_CHECK (0, 5))
    int screenCount = qApp->screens().count(a);#else
    int screenCount = qApp->desktop() - >screenCount(a);#endif

    if (screenCount > 1) {
        // Find the current mouse screen
        QPoint pos = QCursor::pos(a);for (int i = 0; i < screenCount; ++i) {
#if(QT_VERSION > = QT_VERSION_CHECK (0, 5))
            if (qApp->screens().at(i)->geometry().contains(pos)) {
#else
            if (qApp->desktop() - >screenGeometry(i).contains(pos)) {
#endif
                screenIndex = i;
                break; }}}return screenIndex;
}

// Get the current screen size area
QRect QUIHelper::getScreenRect(bool available)
{
    QRect rect;
    int screenIndex = QUIHelper::getScreenIndex(a);if (available) {
#if(QT_VERSION > = QT_VERSION_CHECK (0, 5))
        rect = qApp->screens().at(screenIndex)->availableGeometry(a);#else
        rect = qApp->desktop() - >availableGeometry(screenIndex);
#endif
    } else {
#if(QT_VERSION > = QT_VERSION_CHECK (0, 5))
        rect = qApp->screens().at(screenIndex)->geometry(a);#else
        rect = qApp->desktop() - >screenGeometry(screenIndex);
#endif
    }
    return rect;
}
Copy the code
  1. The QRegExp class has been moved to the Core5Compat module and needs to actively introduce the header file #include.

  2. The QWheelEvent construction parameter and corresponding computed azimuth function have changed.

// Simulate the mouse wheel
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
QWheelEvent wheelEvent(QPoint(0.0), -scal, Qt::LeftButton, Qt::NoModifier);
#else
QWheelEvent wheelEvent(QPointF(0.0), QPointF(0.0), QPoint(0.0), QPoint(0, -scal), Qt::LeftButton, Qt::NoModifier, Qt::ScrollBegin, false);
#endif
QApplication::sendEvent(widget, &wheelEvent);

// Mouse wheel to modify the value directly
QWheelEvent *whellEvent = (QWheelEvent *)event;
*8 is the scrolling distance of the mouse
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
int degrees = whellEvent->delta(a) /8;
#else
int degrees = whellEvent->angleDelta().x(a) /8;
#endif
// The number of steps to scroll,*15 is the Angle of mouse scrolling
int steps = degrees / 15;
Copy the code
  1. QVariantValue changed to qvariant_cast and qVariantSetValue(v, value) changed to v.setValue(val). Going back to the original method, look at the qVariantValue source code package is qvariant_cast.

  2. QStyleOption init has been changed to initFrom.

  3. QVariant::Type is replaced with QMetaType::Type, which itself encapsulates QMetaType::Type.

  4. QStyleOptionViewItemV2, V3, V4 and so on are all gone. QStyleOptionViewItem can be replaced for now.

  5. An overloaded function of QFont’s resolve was replaced with resolveMask.

  6. QSettings setIniCodec method removed, default is UTF8, no setting required.

  7. The QcomboBox activated(QString) and currentIndexChanged(QString) signals remove the one that uses the int index parameter and then retrieves the value by index itself. Personally, I don’t think it’s necessary to delete this.

  8. The QtScript module is completely gone, although it was warned to be a deprecated module in late Qt5 versions, and it was not officially deprecated until Qt6, when all json data parsing was replaced with qJSON class parsing.

  9. To pass QByteArray, add.toutf8 () to the argument. The previous QString parameter is converted to.toutf8 () to compare.

  10. The time conversion function toTime_t + setTime_t of QDateTime was renamed toSecsSinceEpoch + setSecsSinceEpoch. These two methods were added in Qt5.8.

  11. QLabel’s pixmap function, which used to refer to *pixmap(), now references pixmap().

  12. QTableWidget’s sortByColumn method removes the default ascending method, requiring a second parameter to indicate whether it is ascending or descending.

  13. Error in qtNetwork was replaced with errorOccurred.

  14. The XmlPatterns module is gone, and it’s all reparsed with the XML module.

  15. The parameter type of nativeEvent changed.

#if(QT_VERSION > = QT_VERSION_CHECK (0, 6))
bool nativeEvent(const QByteArray &eventType, void *message, qintptr *result);
#else
bool nativeEvent(const QByteArray &eventType, void *message, long *result);
#endif
Copy the code