Search code examples
qt5qt6

Qt6 and Qt5 backward compatibility


Let's say I have Qt5 projects. Qt6 is released and I want to move my projects to it, but I want to have possibility to build my projects with Qt5 too. I know that Qt5 API is incompatible with Qt6. So my question is Qt6 API backward compatible with Qt5 API? If I will fix all issues to build with Qt6 will projects build with Qt5?

For example, in Qt6 there is no QVBoxLayout::setMargin() method but exists QLayout::setContentsMargins(). That means that Qt6 in this case is backward compatible with Qt5.

But do there exist possible underwater stones in backward compatibility with Qt5?


Solution

  • Compatibility isn't black-and-white. Majority of Qt 5 code (in terms of lines of code) is compatible with Qt 6, but some specific constructs you may use are not. That doesn't automatically make things "incompatible", since there may be other constructs you can use that will work in both Qt 5 and Qt 6. And furthermore, the degree of incompatibility that any particular project runs into highly depends on the project's scope, the idiomatic-ness of the design, etc.

    1. In your current Qt 5 project, add the QT_DISABLE_DEPRECATED_BEFORE=0x060000 define to the project. This will disable all obsolete APIs you may be using. I imagine there'll be several errors that you'll have to fix. Read up on the code migration tools that may be available in Qt 6. This is a preparatory step: it won't make your code incompatible with Qt 5, it will just disable all the parts of Qt 5 that were removed in Qt 6 but for which there are cross-compatible alternatives that work in both Qt 5 and Qt 6.

    2. Then, build the project under Qt 6 and see what the problems are. There are no changes needed to your qmake project file (most likely), but you'd need to select Qt 6 in the CMakeLists.txt if you use cmake.

    3. Work around the problems in the simplest fashion at first - using preprocessor conditionals - you'll hopefully have to deal with only a few places where this is necessary.

    4. Now that you have a project that builds and hopefully mostly works in both Qt 5 and Qt 6, you can evaluate whether you could refactor the code to come up with a cross-compatible variant, or perhaps you could factor out some methods/classes that abstract out the incompatible changes that you experienced, so the preprocessor defines will be concentrated in one module vs. sprinkled all over the place.