Reader small image

You're reading from  Qt 6 C++ GUI Programming Cookbook - Third Edition

Product typeBook
Published inApr 2024
PublisherPackt
ISBN-139781805122630
Edition3rd Edition
Right arrow
Author (1)
Lee Zhi Eng
Lee Zhi Eng
author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng

Right arrow

Transitioning from Qt 5 to Qt 6

In this chapter, we will learn about the changes that have been made in Qt 6 and how you can upgrade your existing Qt 5 project to Qt 6. Unlike previous updates, Qt 6 is almost a complete rewrite of the entire Qt code base from the ground up, including all the underlying classes. Such major changes may break your existing Qt 5 project if you simply switch over to Qt 6.

In this chapter, we’re going to cover the following main topics:

  • Changes in C++ classes
  • Using Clazy checks for Clang and C++
  • Changes in QML types

Technical requirements

The technical requirements for this chapter include Qt 6.6.1 MinGW 64-bit, Qt 5.15.2 MinGW 64-bit, and Qt Creator 12.0.2. All the code used in this chapter can be downloaded from the following GitHub repository: https://github.com/PacktPublishing/QT6-C-GUI-Programming-Cookbook---Third-Edition-/tree/main/Chapter06.

Changes in C++ classes

In this recipe, we will learn what the changes in Qt6’s C++ classes are.

How to do it…

Follow these steps to learn about C++ classes in Qt6:

  1. Create a new Qt Console Application by going to File | New Project.
  2. We will open up the main.cpp file and add the following headers:
    #include <QCoreApplication>
    #include <QDebug>
    #include <QLinkedList>
    #include <QRegExp>
    #include <QStringView>
    #include <QTextCodec>
    #include <QTextEncoder>
    #include <QTextDecoder>
  3. After that, add the following code for demonstrating the QLinkedList class:
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        // QLinkedList
        QLinkedList<QString> list;
        list << "string1" << "string2" << "string3";
        QLinkedList<QString>::iterator...

Using Clazy checks for Clang and C++

In this chapter, we will learn how to use the Clazy checks from the Clang toolset to automatically display warnings when obsolete Qt 5 classes and functions are detected in your Qt project.

How to do it…

Let’s get started by following these steps:

  1. We will use the same project from the preceding example. Then, proceed to open up the preferences window by going to Edit | Preferences….
  2. After that, go to the Analyzer page and click on the button beside Diagnostic configuration:
Figure 6.1 – Open up the Diagnostic configuration window

Figure 6.1 – Open up the Diagnostic configuration window

  1. Select the Default Clang-Tidy and Clazy checks option at the top and click the Copy… button, as shown in Figure 6.2. Give it a name and click OK. The new option will now appear under the Custom category:
Figure 6.2 - Click on the Copy button

Figure 6.2 - Click on the Copy button

  1. Then, open the Clazy Checks tab, enable the following...

Changes in QML types

In this chapter, we will learn what changes have been made in Qt 6 compared to Qt 5.

How to do it…

Let’s get started by following these steps:

  1. Create a new Qt Quick Application by going to File | New Project.
  2. Select the Qt 6.2 option for Minimum required Qt version when defining project details.
Figure 6.5 – Select Qt 6.2 as Minimum required Qt version

Figure 6.5 – Select Qt 6.2 as Minimum required Qt version

  1. Once you have created the project, open up main.qml and add these properties to the file:
    import QtQuick
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
        property variant myColor: "red"
        property url imageFolder: "/images"
  2. Next, add a Rectangle object to main.qml, as shown in the following:
    Rectangle {
        id: rect
     ...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Qt 6 C++ GUI Programming Cookbook - Third Edition
Published in: Apr 2024Publisher: PacktISBN-13: 9781805122630
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng