Porting from Qt 5 to Qt 6 using Clazy checks

If you are looking for some help to port from Qt 5 to Qt 6, look no further. Within the Clazy framework, we've implemented some checks and fixits dedicated to help porting your Qt-based project.
Those checks can be run using Clazy as a compiler plugin, using clazy-standalone on a .json file or from within Qt Creator.

Clazy is a a compiler plugin which allows clang to understand Qt semantics, providing warnings and fixits for a lot of C++ Qt code. Developed by Sergio Martins, it is developed under the KDE umbrella. If you install the Qt Creator binary packages provided, clazy is already included!


Clazy checks dedicated to Qt 6 porting


When possible, the checks provide a fixit, if not, only a warning is generated. The checks have to be run against Qt 5, and the fixed code will only compile with Qt 6. For this reason, the checks need to be run together, and not one after the other as recommended by Clazy.

Here is the list of Clazy checks dedicated to Qt 6 porting:

qt6-deprecated-api-fixes APIs that have been deprecated in Qt 5.15 have been removed from Qt 6. The check spots those APIs and offers a replacement, with a fixit or not, depending on the API.
qt6-header-fixes From Qt 5 to Qt 6, some headers have been moved. This checks replaces the obsolete path with the Qt 6 one.
qt6-qhash-signature This check corrects the signature for qHash, qHashBits, qHashRange and qHashRangeCommutative.
qt6-fwd-fixes This check warns against forward declarations that are present in <QtCore/qcontainerfwd.h>. The forward declaration is removed, <QtCore/qcontainerfwd.h> is included instead, if not already included.
missing-qobject-macro This check finds QObject derived classes that don't have a Q_OBJECT macro.

 

Let's now explore how to run those checks on a project.

Running the porting checks using Clazy as a plugin

First, you need to get Clazy or make sure your version is up to date. Version 1.10 will contain a corrected check for the deprecated API fixes, in the mean time please use the master branch.

Then you need to set up your project to run with Clazy.
If you are using qmake add the following to you command line, as appropriate to your OS:

-spec linux-clang QMAKE_CXX="clazy"
-spec macx-clang QMAKE_CXX="clazy"

For Windows with MSVC add:

QMAKE_CXX="clazy-cl.bat".

If you are using cmake add:

--DCMAKE_CXX_COMPILER=clazy

to your command line.

To enable only the checks related to Qt 6 porting use CLAZY_CHECKS:

export CLAZY_CHECKS="qt6-deprecated-api-fixes, qt6-header-fixes,
qt6-qhash-signature, qt6-fwd-fixes,
missing-qobject-macro"

To enable the fixits:

export CLAZY_EXPORT_FIXES=ON

Also set the directories to be ignored by Clazy:

export CLAZY_IGNORE_DIRS=.*lib_dir.*

This will prevent Clazy checks from running on the libraries' files.

Run qmake or cmake, and compile your project. For each source file, a
<source_file_name>.clazy.yaml file is created at the source file location. It contains the summary of the checks' results and the fixits' instructions, for the source file and the header files it includes.

To apply the fixits run clang-apply-replacements on the path to your project directory. Doing this modifies the source and header files, consider backing up your code. If there are conflicts in the fixits, you are informed, and nothing is modified.

Applying Clazy checks and fixits within Qt Creator

In order to have all the Clazy checks dedicated to Qt 6 porting, you need to have a Qt Creator version 14.4.1 or up.

After opening your project in Qt Creator you need to select the Clazy checks to be run during the analysis. This is done in the 'Options' or 'Preferences' window, under 'Analyzer'. Once the checks are selected and saved within a new configuration you can run the analysis. The result of the checks are listed in a dedicated frame. Select the fixit and apply it.

For a detailed description on how to run the Clazy checks in Qt Creator please visit this page.

Within Qt Creator the conflicts between fixits are not warned against. You have to be careful when applying fixits related to different checks happening on a same line.

Further reading

A full description of Clazy - how to get it but also how to apply the checks and fixits - can be found here.

Instructions on how to use clazy-standalone are here.

We also have a page dedicated to Qt 6 porting here.

Your input

If you happen to find any, bugs should be reported here and assigned to the author of this post. 

Feedback from users are always welcome! In particular, please let us know if you've found this tool useful or if you see the need for additional checks.

 


Blog Topics:

Comments