Cross-Platform Development with Qt 6 and Modern C++

By Nibedit Dey
    Advance your knowledge in tech with a Packt subscription

  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Chapter 1: Introduction to Qt 6

About this book

Qt is a cross-platform application development framework widely used for developing applications that can run on a wide range of hardware platforms with little to no change in the underlying codebase. If you have basic knowledge of C++ and want to build desktop or mobile applications with a modern graphical user interface (GUI), Qt is the right choice for you. Cross-Platform Development with Qt 6 and Modern C++ helps you understand why Qt is one of the favorite GUI frameworks adopted by industries worldwide, covering the essentials of programming GUI apps across a multitude of platforms using the standard C++17 and Qt 6 features.

Starting with the fundamentals of the Qt framework, including the features offered by Qt Creator, this practical guide will show you how to create classic user interfaces using Qt Widgets and touch-friendly user interfaces using Qt Quick. As you advance, you'll explore the Qt Creator IDE for developing applications for multiple desktops as well as for embedded and mobile platforms. You will also learn advanced concepts about signals and slots. Finally, the book takes you through debugging and testing your app with Qt Creator IDE.

By the end of this book, you'll be able to build cross-platform applications with a modern GUI along with the speed and power of native apps.

Publication date:
June 2021
Publisher
Packt
Pages
442
ISBN
9781800204584

 

Chapter 1: Introduction to Qt 6

Qt (pronounced cute, not que-tee) is a cross-platform application development framework designed to create great software applications with uniform user interfaces (UIs) for desktop, embedded, and mobile platforms. It provides developers with a great set of tools to design and build great applications without worrying about platform dependency. In this chapter, you will learn the basics about the framework, its history, and how to install Qt on different platforms. You will learn what Qt is and why it is beneficial to use it. By the end of the chapter, you will be able to install Qt and get started on your platform of choice.

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

  • Introducing Qt
  • Reasons for using Qt
  • Downloading and installing Qt
  • Building Qt 6 from source
 

Technical requirements

To get started, you should have a desktop or laptop running on Windows, Linux, or macOS. Please use the updated Windows 10 or Ubuntu 20.04 long-term support (LTS). Alternatively, use the latest version of macOS (newer than macOS 10.14), such as macOS Catalina.

For your integrated development environment (IDE) to run smoothly, your system should have at least an Intel Core i5 processor along with a minimum of 4 gigabytes (GB) of random-access memory (RAM).

You will need an active internet connection to download and install Qt. As a prerequisite, you should also be familiar with C++ as Qt requires C++ programming knowledge.

 

Introducing Qt

Qt is a cross-platform software development framework for desktop, embedded, and mobile platforms. It follows the philosophy of code less, create more, and deploy everywhere. It supports platforms such as Windows, Linux, macOS, VxWorks, QNX, Android, iOS, and so on. The software also supports several microcontroller units (MCUs) from NXP, Renesas, and STMicroelectronics running on bare metal or FreeRTOS.

Qt was born as an attempt to provide a uniform graphical user interface (GUI) with the same look, feel, and functionality across different platforms. Qt accomplishes that by providing a framework to write code once and ensure that it runs on other platforms with minimal or no modifications. It is not a programming language, but rather a framework written in C++. The Qt framework and tools are dual-licensed under open source and commercial licenses.

Qt uses a modular approach to group related functionalities together. Qt Essentials are the foundation of Qt on all platforms. These modules are general and useful for most Qt-based applications. Essential modules are available for open source usage. Examples of Qt Essentials modules are Qt Core, Qt GUI, Qt QML, Qt Widgets, and so on. There are also special-purpose add-on modules that provide specific functionalities and come with certain license obligations. Examples of add-on modules are Qt 3D, Qt Bluetooth, Qt Charts, Qt Data Visualization, and more. As well as this, there are value-added modules such as Qt Automotive Suite, Qt for Device Creation, and Qt for MCUs, among others available under the commercial license.

To find out more about different Qt modules, visit https://doc.qt.io/qt-6/qtmodules.html.

Qt was released for public use in 1995. Since then, there have been many improvements and major changes. Qt 6 is the new major version of Qt. Its main goals are to remain prepared for the requirements coming in 2020 and beyond, remove obsolete modules, and be more maintainable. With this focus, there are architectural changes in Qt 6 that may break some level of backward compatibility with earlier versions.

Some essential modifications in Qt 6 are outlined here:

  • Introduction of strong typing
  • JavaScript as an optional feature of Qt Modeling Language (QML)
  • Removal of QML versioning
  • Removal of the duplicate data structures between QObject and QML
  • Avoidance of the creation of runtime data structures
  • Compilation of QML into efficient C++ and native code
  • Support for hiding implementation details
  • Better integration of tools

Now that we've covered the basics, let's look at the main reasons for using Qt…

 

Reasons for using Qt

Qt is a modular, cross-platform application development framework. The biggest misunderstanding about Qt is that many people consider it as a GUI framework. However, Qt is much more than a GUI framework. It not only comprises a GUI module, but also a set of modules to make application development faster and easier to scale on various platforms. The biggest benefit of using Qt is its ability to provide portability to various platforms. Here are some advantages of using Qt for developers:

  • You can create incredible user experiences for your customers and boost your company brand using Qt.
  • Cross-platform development saves both time and money. You can target multiple platforms with the same code base.
  • Qt is known for making C++ easy and accessible. With Qt, developers can easily create high-performance, scalable applications with a fluid UI.
  • Due to the open source model, the framework is future-proof, along with a great ecosystem.
  • It further supports different programming languages and is a very flexible and reliable framework. Consequently, there are great companies such as Adobe, Microsoft, Samsung, AMD, HP, Philips, and MathWorks using Qt for their applications. Many open source projects such as VLC (previously known as VideoLAN Client), Open Broadcaster Software (OBS), and WPS Office (where WPS stands for Writer, Presentation, and Spreadsheets) are also built on Qt.

The core values of Qt are outlined as follows:

  • Cross-platform nature
  • Highly scalable
  • Very easy to use
  • Built-in with world-class application programming interfaces (APIs), tools, and documentation
  • Maintainable, stable, and compatible
  • A large community of users

Whether you are a hobbyist, a student, or working for a company, Qt provides great flexibility to use its modules as per your requirement. Many universities are using Qt as one of their coursework subjects. So, Qt is a great choice for programmers to start building new applications with ready-made features. Let's start by downloading and installing Qt 6 on your machine.

 

Downloading and installing Qt

There are different ways to install the Qt framework and tools on your system. You can download an online or offline installer from the Qt website, or you can build the source packages yourself. Qt recommends using the online installer for first-time installations and the Qt Maintenance Tool for modifying the installation later.

The installers allow you to download and install the following components:

  • Qt libraries
  • Qt Creator IDE
  • Documentation and examples
  • Qt source code
  • Add-On modules

The online installer allows you to select open source or commercial versions of Qt, tools, and Add-On modules to install based on the chosen license. The online installer doesn't contain the Qt components, but it is a downloader client to download all the relevant files. You can install once the download is complete. You will require a Qt account to download and install Qt. An evaluation version for the commercial Qt gives you free trial-period access, with all commercial packages and access to official Qt support. The installer requires you to sign in with your Qt account. If you don't have a Qt account, you can sign up during the installation process. The installer fetches the license attached to the account from the Qt server and lists down modules according to your license. If you are new to Qt, then we recommend that you start with the open source version.

The offline installer is a platform-specific package that includes all Qt modules and add-ons relevant for the platform. Due to the official policy changes, open source offline installers are not available any more since Qt 5.15. If you have a commercial license, then you can provide the credentials during the installation process. You can locate your license key in your Qt account web portal.

You can download them from the following links:

Downloading Qt

Let's start by downloading Qt onto your machine, as follows:

  1. To begin, visit the https://www.qt.io/download download page.
  2. Click on the Download. Try. Buy. button in the top-right corner. You will see different download options here.
  3. If you want to try the commercial version, then click on Try Qt section. If you already have a Qt account, then you can log in into the account under the Existing customers section.
  4. Considering that you are new to Qt, we will begin with the open source version. Click on the Go open source button, as shown in the following screenshot:
    Figure 1.1 – Qt website download options

    Figure 1.1 – Qt website download options

  5. On the next screen, you will find Download the Qt Online Installer button. Click on it to proceed to the download link.
  6. The web page will automatically detect the underlying platform details from the browser and will show you the Download button. You can also select your intended installer by choosing the other options: you can select 32-bit or 64-bit or download for a different platform.

    You will see a Thank you page after you click on the download option. At this stage, you can find the installer in your download folder.

Next, let's begin with the installation process on the Windows platform.

Installing Qt on Windows

Now, let's start the installation process on Windows! Proceed as follows:

  1. You will find a file with the name qt-unified-windows-x86-%VERSION%-online.exe inside your download folder. Double-click on the executable, and you will see a Welcome screen.
  2. Click the Next button, and a credentials screen will appear, asking you to log in with your Qt account. If you don't have one, then you can sign up on the same page, as shown in the following screenshot:
    Figure 1.2 – Login screen of the installer

    Figure 1.2 – Login screen of the installer

  3. In the next screen, you will be presented with the open source usage obligations agreement. You won't get this screen if you are installing using a commercial license. Click on the first checkbox, saying I have read and approve the obligations of using Open Source Qt, and acknowledge that you are not using Qt for commercial purposes. Make sure you read the terms and conditions mentioned in the agreement! Then, click on the Next button.
  4. The next screen will provide you with options related to tracking and sharing pseudonymous data in Qt Creator. You may allow or disable these options based on your preferences. Then, click on the Next button to proceed to the next screen.
  5. In the next screen, you can specify the installation path. You may continue with the default path, or you can change it to any other path if you don't have enough space on the default drive. You can also choose whether you want to associate common file types with Qt Creator by selecting the checkbox option at the bottom. Click on the Next button.
  6. Next, you will be presented with a list where you can select the version(s) of Qt you need to install on your system. You may simply proceed with the default options. If you don't need some of the components, then you can unselect them to reduce the size of the download. You can update the Qt components using the Maintenance Tool anytime later. To complete the installation process, click on the Next button. The component selection screen can be seen here:
    Figure 1.3 – Component selection screen of the installer

    Figure 1.3 – Component selection screen of the installer

  7. In the next screen, you will be presented with the license agreement. Click on the first radio button, which says I have read and agree to the terms contained in the license agreements. Again, make sure you read the terms and conditions mentioned in the license agreement, and then click on the Next button.
  8. On the next screen, you can create Start menu shortcuts on Windows. This screen will not be available for other platforms. Once you have finished doing this, click on the Next button.
  9. Now, Qt is ready to be installed in your system. Make sure you have a working internet connection and data balance. Click on the Install button to begin the installation. The download process will take time, depending on your internet speed. Once the required files are downloaded, the installer will automatically install them in the previously selected path.
  10. Once the installation is finished, the installer will make an entry for the Maintenance Tool, which will help you later to make changes to the libraries. Click on the Next button to move to the last screen of the installer.
  11. In order to complete the installation process, click on the Finish button. If you have left the Launch Qt Creator checkbox checked, then Qt Creator will be launched. We will discuss this in more detail in the next chapter. Now, Qt is ready to be used on your Windows machine. Click on the Finish button to exit the wizard.

Installing Qt on Linux

Now, let's install the Qt framework on the latest LTS version of Linux, such as Ubuntu 20.04, CentOS 8.1, or openSUSE 15.1. We will be focusing on the most popular Linux distribution, Ubuntu. You can follow the same steps as mentioned previously to download the online installer from the Qt website.

On Ubuntu, you will get an installer file such as qt-unified-linux-x64-%VERSION%-online.run, where %VERSION% is the latest version— for example: qt-unified-linux-x86-4.0.1-1-online.run.

  1. You may have to give write permissions to the downloaded file before executing it. To do that, open the terminal and run the following command:
    $ chmod +x qt-unified-linux-x64-%VERSION%-online.run
  2. You can start the install process by double-clicking the downloaded installer file. The installation requires superuser access. You may have to add a password in the authorization dialog during the installation. You can also run the installer from the terminal, as follows:
    $ ./qt-unified-linux-x64-%VERSION%-online.run
  3. You will see similar screens to those shown for the Windows platform. Apart from the operating system (OS)-specific title bar changes, all the screens remain the same for installation in Ubuntu or similar Linux flavors.

At the time of writing the book, there was no Ubuntu or Debian package available for Qt 6 as the respective maintainers have stepped down. Hence, you may not get the Qt 6 package from the terminal.

Installing Qt on macOS

If you are a macOS user, then you can also install the same way as discussed for the earlier platforms. You can follow the same steps mentioned previously to download the online installer from the Qt website.

You will get an installer file such as qt-unified-mac-x64-%VERSION%-online.dmg, where %VERSION% is the latest version (such as qt-unified-mac-x64-4.0.1-1-online.dmg).

Qt has a dependency on Xcode. To install Qt on your Mac, you will need Xcode installed on your machine, otherwise, it will refuse to install. If you are an Apple developer, then your Mac may have Xcode installed. If you don't have Xcode installed on your machine, then you may proceed to install Xcode's Command Line Tools instead of Xcode. This will save time and storage space on your machine:

  1. To begin, type the following command on the terminal:
    $ xcode-select --install    
  2. If the terminal shows the following output, then your system is ready for the next steps:
    xcode-select: error: command line tools are already installed, use
    "Software Update" to install updates
  3. The next step is to install the Qt framework. Double-click on the installer file to launch the installation interface.
  4. If the installer still complains that Xcode is not installed, then keep clicking OK until the message goes away permanently. Remember the installation path. Once the installation is finished, you are ready to use Qt on your machine.

Further instructions on Qt for macOS can be found at the following link:

https://doc.qt.io/qt-6/macos.html

Updating or removing Qt

Once Qt is installed, you can modify the components—including updating, adding, and removing components—using the Maintenance Tool under the installation directory. The directory structure remains the same for all desktop platforms. The installation directory contains folders and files, as shown in the following screenshot (on Windows):

Figure 1.4 – The Maintenance Tool inside the installed folder

Figure 1.4 – The Maintenance Tool inside the installed folder

Let's begin with the maintenance process! You can add, remove, and update modules using the Maintenance Tool. Proceed as follows:

  1. Click on the MaintenanceTool.exe executable to launch the maintenance interface. Click on the Next button, and a credentials screen will appear, asking you to log in with your Qt account. The login details will be prefilled from your last login session. You can click Next to add or update components or select the Uninstall only checkbox to remove Qt from your system. The following screenshot shows what the credentials screen looks like:
    Figure 1.5 – Welcome screen of the Maintenance Tool

    Figure 1.5 – Welcome screen of the Maintenance Tool

  2. Once you are logged in, the tool will present you with options to add or remove or update the components, as shown in the following screenshot. Click on the Next button to proceed further:
    Figure 1.6 – Setup screen of the Maintenance Tool

    Figure 1.6 – Setup screen of the Maintenance Tool

  3. On the next screen, you can select new components from the latest releases or the archived version. You can click on the Filter button to filter versions as per your requirement. You can also add new platform-specific components such as Android from the component list. If the component is existing and you uncheck it, then it will be removed from your desktop during the update. Once you have selected the components, click on the Next button. The following screenshot shows what the component selection screen looks like:
    Figure 1.7 – Component selection screen

    Figure 1.7 – Component selection screen

  4. You will then come across the update screen. This screen will tell you how much storage space is required for the installation. If you are running out of storage space, then you may go back and remove some existing components. Click on the Update button to begin the process, as illustrated in the following screenshot:
    Figure 1.8 – The Ready to Update screen of the Maintenance Tool

    Figure 1.8 – The Ready to Update screen of the Maintenance Tool

  5. You can abort the update installation process by clicking on the Cancel button. Qt will warn you and ask you for confirmation before aborting the installation process, as illustrated in the following screenshot. Once the process is aborted, click on the Next button to exit the wizard:
    Figure 1.9 – The cancel dialog

    Figure 1.9 – The cancel dialog

  6. Launch the Maintenance Tool again to update existing components from the latest releases. You can click on the Quit button to exit the Maintenance Tool. Please wait while the installer fetches the meta-information from the remote repository. Click on the Next button to see the available components. The update option is illustrated in the following screenshot:
    Figure 1.10 – The Update option in the Maintenance Tool

    Figure 1.10 – The Update option in the Maintenance Tool

  7. Next, you can select which components to update from the checkboxes. You can choose to update all or you can update selectively. The installer will show how much storage space will be required for the update, as illustrated in the following screenshot. You can click Next to go to the update screen and begin the update. Then, on the next screen, click on the Update button to download the update packages:
    Figure 1.11 – Components available for update

    Figure 1.11 – Components available for update

  8. Once the installation is finished, the installer makes entries for the Maintenance Tool, which will help you make changes to the libraries later. This is illustrated in the following screenshot. Click on the Next button to move to the last screen of the installer:
    Figure 1.12 – The Update finished screen in the Maintenance Tool

    Figure 1.12 – The Update finished screen in the Maintenance Tool

  9. In the last screen, you will see Restart and Finish buttons. Click on the Finish button to exit the Qt wizard.
  10. Similarly, you can restart or launch the Maintenance Tool and select the Remove all components radio button. Click on the Next button to begin the uninstallation process, as illustrated in the following screenshot:
Figure 1.13 – The Remove option in the Maintenance Tool

Figure 1.13 – The Remove option in the Maintenance Tool

Please note that on clicking the Uninstall button, all the Qt components will be removed from your system; you will have to reinstall Qt if you want to use them again. Click on Cancel if you don't intend to remove the Qt components from your system, as illustrated in the following screenshot. If you intend to remove the existing version and use a newer version of Qt, then select the Add or remove components option, as discussed earlier. This will remove older Qt modules and free up your disk space:

Figure 1.14 – The Uninstall screen in the Maintenance Tool

Figure 1.14 – The Uninstall screen in the Maintenance Tool

In this section, we learned about modifying an existing Qt installation through the Maintenance Tool. Now, let's learn how to build and install Qt from the source code.

 

Building Qt 6 from source

If you want to build the framework and tools yourself or experiment with the latest unreleased code, then you can build Qt from the source code. If you're going to develop a specific Qt version from the source, then you can download the Qt 6 source code from the official releases link, as shown here: https://download.qt.io/official_releases/qt/6.0/.

If you are a commercial customer, then you can download the Source Packages from your Qt account portal. Platform-specific building instructions are discussed in the upcoming subsections.

You can also clone from the GitHub repository, and check out the desired branch. At the time of authoring this book, the Qt 6 branch remained inside the Qt 5 super module. You can clone the repository from the following link: git://code.qt.io/qt/qt5.git.

The qt5.git repository may get renamed to qt.git in the future for maintainability. Please refer to the QTQAINFRA-4200 Qt ticket. Detailed instructions on how to build Qt from Git can be found at the following link: https://wiki.qt.io/Building_Qt_6_from_Git.

Ensure that you install the latest versions of Git, Perl, and Python on your machine. Make sure there is a working C++ compiler before proceeding to the platform-specific instructions in the next section.

Installing Qt on Windows from source

To install Qt 6 on Windows from source code, follow these next steps:

  1. First of all, download the source code from Git or from the open source download link mentioned earlier. You will get a compressed file as qt-everywhere-src--%VERSION%.zip, where %VERSION% is the latest version (such as qt-everywhere-src-6.0.3.zip). Please note that suffixes such as -everywhere-src- may get removed in the future.
  2. Once you have downloaded the source archive, extract it to a desired directory—for example, C:\Qt6\src.
  3. In the next step, configure the build environment with a supported compiler and the required build tools.
  4. Then, add the respective installation directories of CMake, ninja, Perl, and Python to your PATH environment variable.
  5. The next step is to build the Qt library. To configure the Qt library for your machine type, run the configure.bat script in the source directory.
  6. In this step, build Qt by typing the following command in Command Prompt:
    >cmake --build . –parallel
  7. Next, enter the following command in Command Prompt to install Qt on your machine:
    >cmake --install .

Your Windows machine is now ready to use Qt.

To understand more about the configure options, visit the following link:

https://doc.qt.io/qt-6/configure-options.html

Detailed build instructions can be found at the following link:

https://doc.qt.io/qt-6/windows-building.html

Installing Qt on Linux from source

To build the source package on Linux distributions, run the following set of instructions on your terminal:

  1. First of all, download the source code from Git or from the open source download link mentioned earlier. You will get a compressed file as qt-everywhere-src--%VERSION%.tar.xz, where %VERSION% is the latest version (such as qt-everywhere-src-6.0.3.tar.xz). Please note that suffixes such as -everywhere-src- may get removed in the future.
  2. Once you have downloaded the source archive, uncompress the archive and unpack it to a desired directory—for example, /qt6, as illustrated in the following code snippet:
    $ cd /qt6
    $ tar xvf qt-everywhere-opensource-src-%VERSION%.tar.xz 
    $ cd /qt6/qt-everywhere-opensource-src-%VERSION%
  3. To configure the Qt library for your machine, run the ./configure script in the source directory, as illustrated in the following code snippet:
    $ ./configure
  4. To create the library and compile all the examples, tools, and tutorials, type the following commands:
    $ cmake --build . --parallel
    $ cmake --install .
  5. The next step is to set the environment variables. In .profile (if your shell is bash, ksh, zsh, or sh), add the following lines of code:
    PATH=/usr/local/Qt-%VERSION%/bin:$PATH
    export PATH

    In .login (if your shell is csh or tcsh), add the following line of code:

    setenv PATH /usr/local/Qt-%VERSION%/bin:$PATH

If you use a different shell, modify your environment variables accordingly. Qt is now ready to be used on your Linux machine.

Detailed building instructions for Linux/X11 can be found at the following link:

https://doc.qt.io/qt-6/linux-building.html

Installing Qt on macOS from source

Qt has a dependency on Xcode. To install Qt on your Mac, you will need Xcode installed on your machine. If you don't have Xcode installed on your machine, then you may proceed to install Xcode's Command Line Tools:

  1. To begin, type the following command on the terminal:
    $ xcode-select --install    
  2. If the terminal shows the following output, then your system is ready for the next steps:
    xcode-select: error: command line tools are already installed, use
    "Software Update" to install updates
  3. To build the source package, run the following set of instructions on your terminal:
    $ cd /qt6
    $ tar xvf qt-everywhere-opensource-src-%VERSION%.tar          
    $ cd /qt6/qt-everywhere-opensource-src-%VERSION%
  4. To configure the Qt library for your Mac, run the ./configure script in the source directory, as illustrated in the following code snippet:
    $ ./configure  
  5. To create a library, run the make command, as follows:
    $ make
  6. If -prefix is outside the build directory, then type the following lines to install the library:
    $ sudo make -j1 install
  7. The next step is to set the environment variables. In .profile (if your shell is bash), add the following lines of code:
    PATH=/usr/local/Qt-%VERSION%/bin:$PATH
    export PATH

    In .login (if your shell is csh or tcsh), add the following line of code:

    setenv PATH /usr/local/Qt-%VERSION%/bin:$PATH

Your machine is now ready for Qt programming.

Detailed building instructions for macOS can be found here:

https://doc.qt.io/qt-6/macos-building.html

In this section, we learned how to install Qt from source on your favorite platform. Now, let's summarize our learning.

 

Summary

This chapter explained the basics of the Qt framework and what it can be used for. Here, we discussed the history, different modules, and advantages of using Qt. We also learned about different methods of installation with license obligations, giving step-by-step installation procedures for Qt on different desktop platforms. Now, your machine is ready to explore Qt.

In the next chapter, we will discuss the Qt Creator IDE. You will learn about the UI of the IDE, different configurations, and how to use it for your Qt project.

About the Author

  • Nibedit Dey

    Nibedit Dey is a software engineer turned serial entrepreneur with over a decade of experience in building complex software-based products with amazing user interfaces.

    Before starting his entrepreneurial journey, he worked for Larsen and Toubro and Tektronix in different R&D roles. He holds a bachelor's degree in biomedical engineering and a master's degree in digital design and embedded systems. Specializing in Qt and embedded technologies, his current role involves end-to-end ownership of products right from architecture to delivery. Currently, he manages two technology-driven product startups named ibrum technologies and AIDIA Health. He is a tech-savvy developer who is passionate about embracing new technologies.

    Browse publications by this author
Cross-Platform Development with Qt 6 and Modern C++
Unlock this book and the full library for FREE
Start free trial