Book Image

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

By : Nibedit Dey
Book Image

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

By: Nibedit Dey

Overview of 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.
Table of Contents (17 chapters)
1
Section 1: The Basics
6
Section 2: Cross-Platform Development
8
Section 3: Advanced Programming, Debugging, and Deployment

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.