Chapter 5: Cross-Platform Development

Qt has been well known for its cross-platform capability since its initial release—it was the primary vision behind creating this framework. You can use Qt Creator on your favorite desktop platforms such as Windows, Linux, and macOS, and create fluid, modern, touch-friendly graphical user interfaces (GUIs) and desktop, mobile, or embedded applications with the same code base or with a little modification. You can easily modify your code and deploy it on a target platform. Qt has several built-in tools to analyze your application and its performance on various supported platforms. Furthermore, it's easy to use and has an intuitive user interface (UI), unlike with other cross-platform frameworks.

In this chapter, you will learn cross-platform development essentials and how to build applications on different platforms. With this, you will be able to run sample applications on your favorite desktop and mobile platforms.

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

  • Understanding cross-platform development
  • Understanding compilers
  • Building with qmake
  • Qt project (.pro) files
  • Understanding build settings
  • Platform-specific settings
  • Using Qt with Microsoft Visual Studio
  • Running a Qt application on Linux
  • Running a Qt application on macOS and iOS
  • Other Qt-supported platforms
  • Porting from Qt 5 into Qt 6

By the end of this chapter, you will understand Qt project files, essential settings, and how to run your Qt application on a mobile device. Let's get started!

Technical requirements

The technical requirements for this chapter include minimum versions of Qt 6.0.0 and Qt Creator 4.14.0 installed on a latest desktop platform such as Windows 10, Ubuntu 20.04, or macOS 10.14.

All the code used in this chapter can be downloaded from the following GitHub link:

https://github.com/PacktPublishing/Cross-Platform-Development-with-Qt-6-and-Modern-Cpp/tree/master/Chapter05/HelloWorld

Important note

The screenshots used in this chapter are taken on the Windows platform. You will see similar screens based on the underlying platforms in your machine.

Understanding cross-platform development

There are several cross-platform frameworks available on the market, but Qt is a better option to select owing to its maturity and available community support. It's easy for a traditional C++ developer to adapt to Qt faster and develop high-quality applications. The Qt framework allows developers to develop applications that are compatible with multiple platforms such as Windows, Linux, macOS, QNX (originally known as Quick Unix [Qunix]), iOS, and Android. It facilitates faster application development with better code quality, with its ability to code once and its deploy-anywhere philosophy. Qt handles platform-specific implementations internally, and also enables you to build amazing ultra-lightweight applications with an impressive GUI on microcontroller-powered devices.

To develop applications using Qt for embedded platforms, you will require a commercial license to use Qt for Device Creation. Qt also supports some of the microcontroller unit (MCU) platforms such as Renesas, STM32, and NXP. At the time of writing this book, Qt for MCUs 1.8 was launched, which provides ultra-lightweight modules with a small memory footprint.

Some advantages of cross-platform development using the Qt framework are listed here:

  • Cost efficiency with reduced cost of development
  • Better code reusability
  • Convenience
  • Faster time to market (TTM)
  • Wider market reach
  • Delivers a near-native experience
  • High on performance

There are also some disadvantages, such as these:

  • Unavailability of platform-specific features and access to all platform application programming interfaces (APIs)
  • Communication challenges between native and non-native components
  • Certain device-specific features and hardware-compatibility challenges
  • Delayed platform updates

In this section, you got a basic idea of the cross-platform nature of Qt and learned about the pros and cons of cross-platform development. Before you can run an application on any platform, you will need a compiler to compile an application for a target platform. In the next section, we will learn about compilers supported by the Qt framework.

Understanding compilers

In this section, you will learn what a compiler is and how to use it for cross-platform development. A compiler is a piece of software that transforms your program into machine code or low-level instructions that can be read and executed by a computer. These low-level machine instructions vary from platform to platform. You can compile Qt applications with different compilers such as the GNU Compiler Collection (GCC), or you can use a vendor-supplied one. In Qt Creator, you can find a compiler supported for a kit under the Kits tab, along with other essential tools for building an application on a particular platform such as Windows, Linux, or macOS. Not all supported compilers are provided with the Qt installer, but you can find the most widely used compilers automatically listed in the recommended kit. Qt may drop support for certain kit configurations or replace them with the latest version.

Currently, Qt supports the following compilers:

  • GCC
  • Minimalist GNU for Windows (MinGW)
  • Microsoft Visual C++ (MSVC)
  • Low Level Virtual Machine (LLVM)
  • Intel C++ Compiler (ICC)
  • Clang and clang-cl
  • Nim
  • QCC

Additionally, the Qt Creator Bare Metal Device plugin offers provision for the following compilers:

  • IAR Embedded Workbench (IAREW)
  • KEIL
  • Small Device C Compiler (SDCC)

Apart from the preceding compilers, Qt uses specific built-in compilers while building a Qt project. These are listed here:

  • Meta-Object Compiler (moc)
  • User Interface Compiler (uic)
  • Resource Compiler (rcc)

You can use the aforementioned compilers to build applications for a target platform or to add a custom compiler configuration. In the next section, you will learn how to create a custom compiler configuration.

Adding custom compilers

To add a compiler that is not automatically detected by Qt Creator or is unavailable, use the Custom option. You can specify the compiler and toolchain paths to the directories and configure these accordingly.

To add a custom compiler configuration, follow these steps:

  1. To create a new compiler configuration in Qt, click on the Tools menu on the menu bar and then select the Kits tab from the left-side pane.
  2. Then, click on the Compilers tab and select Custom from the Add dropdown. You will see C and C++ options in the context menu. Select the type as per your requirement. You can see an overview of this in the following screenshot:
    Figure 5.1 – Custom compiler option

    Figure 5.1 – Custom compiler option

  3. In the next step, complete the Name field with a customized name for the compiler.
  4. Next, in the Compiler path field, select a path to the directory where the compiler is located.
  5. Next, in the Make path field, browse a path to the directory where the make tool is located.
  6. In the next step, specify the application binary interface (ABI) version in the ABI field.

    You can see an overview of this in the following screenshot:

    Figure 5.2 – Required fields for a custom compiler

    Figure 5.2 – Required fields for a custom compiler

  7. Next, you can specify the default required macros in the Predefined macros field. Specify each macro on separate lines in the following format: MACRO[=value].
  8. In the next step, specify in the Header paths field the paths to directories that the compiler checks for headers.
  9. Next, in the C++11 flags field, specify the flags that turn on C++11 support.
  10. In the next step, specify the location of mkspecs (a set of compilation rules) in the Qt mkspecs field.
  11. Next, in the Error parser field, select a suitable error parser.
  12. Click on the Apply button to save the configuration.

In this section, you learned about supported compilers and how to create a new compiler configuration in Qt Creator, but to build and run a project we need more tools than just a compiler. Qt provides qmake as a built-in build tool for our convenience. In the next section, we will discuss what qmake is.

Building with qmake

Make is a build tool that reads project configuration file called a Makefile and builds executable programs and libraries. qmake is a Qt-provided build tool that simplifies the build process for development projects across multiple platforms. It expands the information in each project file to a Makefile that executes the necessary commands for compiling and linking. It can also be used for non-Qt projects. qmake generates a Makefile based on the information in a project file, and contains supplementary features to support development with Qt, automatically including build rules for moc and uic. qmake can also create projects for Microsoft Visual Studio without requiring the developer to change the project file.

Being a community-driven framework, Qt is really flexible toward developers and gives them the freedom to choose the most suitable tools for their project, without forcing them to use its own build system. Qt supports the following types of build systems:

  • qmake
  • CMake
  • Qbs
  • Meson
  • Incredibuild

You can run qmake from the Qt Creator UI or from the command line. You should run qmake every time you make changes to your project files. Here is the syntax to run qmake from the command line:

>qmake [mode] [options] files

qmake provisions two different modes of operation. In the default mode, qmake uses the information in a project file to generate a Makefile, but it can also generate project files. The modes are listed as follows:

  • -makefile
  • -project

In Makefile mode, qmake will generate a Makefile that is used to build the project. The syntax to run qmake in Makefile mode is shown here:

>qmake -makefile [options] files

In project mode, qmake will generate a project file. The syntax to run qmake in project mode is shown here:

>qmake -project [options] files

If you use Visual Studio as an Integrated Development Environment (IDE), then you can import an existing qmake project into Visual Studio. qmake can create a Visual Studio project that contains all the essential information required by the development environment. It can recursively generate .vcproj files in subdirectories and a .sln file in the main directory, with the following command:

>qmake -tp vc -r

For example, you can generate a Visual Studio project for your HelloWorld project by running this command:

>qmake -tp vc HelloWorld.pro

Please note that every time you modify your project file, you need to run qmake to generate an updated Visual Studio project.

You can find more details about qmake at the following link:

https://doc.qt.io/qt-6/qmake-manual.html

Most qmake project files define the source and header files used by a project, using a list of name = value and name += value definitions, but there are additional advanced features in qmake that use other operators, functions, platform scope, and conditions to create a cross-platform application. Further details of the qmake language can be found at the following link: https://doc.qt.io/qt-6/qmake-language.html.

The Qt team has put a lot of effort into Qt 6 to make it future-proof by moving to a broadly adopted, popular build tool: CMake. There were changes implemented to make Qt more modular by using Conan as a package manager for some of the add-ons. Some of the Qt modules in Qt 6 are no longer available as binary packages in the Qt online installer but are available as Conan recipes. You can learn more about the build system changes and the addition of CMake as the default build tool at the following link: https://doc.qt.io/qt-6/qt6-buildsystem.html.

Important note

In Qt 5, the build system was made on top of qmake, but in Qt 6, CMake is the build system for building Qt from the source code. This change only affects developers who want to build Qt from sources. You can still use qmake as a build tool for your Qt applications.

In this section, you learned about qmake. We are skipping advanced qmake topics for self-exploration. In the next section, we will discuss Qt project files, which are parsed by qmake.

Qt Project (.pro) files

The .pro files created by Qt Creator in the earlier examples are actually Qt project files. A .pro file contains all the information required by qmake to build an application, a library, or a plugin. A project file supports both simple and complex build systems. A simple project file may use straightforward declarations, defining standard variables to indicate the source and header files that are used in a project. Complex projects may use multiple flow structures to optimize the build process. A project file contains a series of declarations to specify resources, such as links to the source and header files, libraries required by a project, custom-build processes for different platforms, and so on.

A Qt project file has several sections and uses certain predefined qmake variables. Let's have a look here at our earlier HelloWorld example .pro file:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses

# deprecated APIs.

# In order to do so, uncomment the following line.

#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    

# disables all the APIs deprecated before Qt 6.0.0

SOURCES +=

    main.cpp

    widget.cpp

HEADERS +=

    widget.h

FORMS +=

    widget.ui

# Default rules for deployment.

qnx: target.path = /tmp/$${TARGET}/bin

else: unix:!android: target.path = /opt/$${TARGET}/bin

!isEmpty(target.path): INSTALLS += target

The project file simply tells qmake what the required Qt modules in the project are, as well as the name of the executable program. It also links to the header files, source files, form files, and resource files that need to be included in the project. All of this information is crucial in order for qmake to create the configuration files and build the application. For a more complex project, you may configure your project file differently for different operating systems.

The following list describes the most frequently used variables and describes their purpose:

  • QT: A list of Qt modules used in a project
  • CONFIG: General project configuration options
  • DESTDIR: The directory in which the executable or binary file will be placed
  • FORMS: A list of UI files to be processed by the UI compiler (uic)
  • HEADERS: A list of filenames of header (.h) files used when building a project
  • RESOURCES: A list of resource (.qrc) files to be included in the final project
  • SOURCES: A list of source code (.cpp) files to be used when building a project
  • TEMPLATE: The template to use for a project

You can add different Qt modules, configurations, and definitions to your project. Let's take a look at how we can accomplish this. To add additional modules, you simply add the module keyword after QT +=, as shown here:

QT += core gui sql

You can also add a condition in front to determine when to add a specific module to your project, as follows:

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

You can also add configuration settings to your project. For example, if you want to specify c++17 specifications while compiling a project, then add the following line to your .pro file:

CONFIG += c++17

You can add a comment to a project file, starting with the hash symbol (#), and the build system will ignore the corresponding line of text. Now, let's have a look at the TEMPLATE variable. This determines whether the output of the build process will be an application, a library, or a plugin. There are different variables available to outline the type of file qmake will generate. These are listed as follows:

  • app is used to build an application.
  • lib is used to build a library.
  • aux is used to build nothing. Use this if no compiler needs to be invoked to create a target—for instance, because your project is written in an interpreted language.
  • subdirs is used for the subdirectories specified using the SUBDIRS variable. Each subdirectory must contain its own project file.
  • vcapp is used to create a Visual Studio project file to build an application.
  • vclib is used to create a Visual Studio project file to build a library.
  • vcsubdirs is used to create a Visual Studio solution file to build projects in subdirectories.

Qt project files sometimes need to depend on the include feature. In a Qt project file, you can also define two significant variables: INCLUDEPATH and DEPENDPATH. You can use the SUBDIRS variable to compile a set of dependent libraries or modules.

Now, let's discuss what a .pri file is.

Understanding differences between .pro and .pri files

You can create a .pri file to include project files in a complex project. This improves readability and segregates different modules. A .pri file is usually called a project include file or a qmake include file, and its format is similar to that of a .pro file. The main difference is in the intent of use; a .pro file is what we expect to run qmake on directly, while a .pri file is included by a .pro file. You can add common configurations such as source files, header files, .ui files, and .qrc files into .pri files and include them from multiple .pro files as per your project needs.

You can include a .pri file inside a .pro file, as illustrated here:

include($$PWD/common.pri)

In this section, you learned about what a Qt project file is, as well as the different variables used in it. In the next section, we will discuss different build settings.

Understanding build settings

Before a project is compiled or built, the compiler requires certain details, which are known as the build settings. This is a very important part of the compilation process. In this section, you will learn about build settings and how to configure them in a proper way. You can have multiple build configurations for the same project. Usually, Qt Creator creates debug, release, and profile build configurations automatically. A debug build contains additional debug symbols required for debugging an application, whereas the release version is an optimized version without such symbols. Generally, developers use a debug configuration for testing and a release configuration for creating the final binaries. A profile build is an optimized release build that is delivered with separate debug information and is best suited to analyzing applications.

Build settings can be specified in the Projects mode. You may find that the Projects button is disabled if there are no projects opened in the IDE. You can add a new build configuration by clicking the Add drop-down button and then selecting the type of configuration you would like to add. The options may depend on the build system selected for the project. You can add multiple build configurations as per your requirement. You can click on the Clone… button to add a build configuration based on the current build configuration, or click on the Rename… button to rename the currently selected build configuration. Click on the Remove button to remove a build configuration.

You can see an overview of this in the following screenshot:

Figure 5.3 – Build settings and Qt Quick Compiler option

Figure 5.3 – Build settings and Qt Quick Compiler option

Normally, Qt Creator builds projects in a different directory from the source directory, known as shadow builds. This segregates the files generated for each build and run kit. If you want to only build and run with a single kit, then you can deselect the Shadow build checkbox. The Qt Creator project wizard creates a Qt Quick project that can be compiled to use the Qt Resource System. To use the default settings, select Leave at Default. To compile Qt Quick code, select Enable in the Qt Quick Compiler field, as shown in Figure 5.3.

You can read more about different build configurations at the following link:

https://doc.qt.io/qtcreator/creator-build-settings.html

In this section, we discussed build settings. While building a cross-platform application, it is important to add platform-specific configurations to the project file. In the next section, we will learn about platform-specific settings.

Platform-specific settings

You can define different configurations for different platforms, since not every configuration can fit all use cases. For example, if you want to include different header paths for different operating systems, you can add the following lines of code to your .pro file:

win32: INCLUDEPATH += "C:/mylibs/windows_headers"

unix:INCLUDEPATH += "/home/user/linux_headers"

In the preceding code snippet, we have added some Windows-specific and Linux-specific header files. You can also put configurations such as if statements in C++, as shown here:

win32 {

    SOURCES += windows_code.cpp

}

The preceding code is intended only for Windows platforms, which is why we have added a win32 keyword before it. If your target platform is based on Linux, then you can add a unix keyword to add Linux-specific configurations.

To set a custom icon for your application on the Windows platform, you should add the following line of code to your project (.pro) file:

RC_ICONS = myapplication.ico

To set a custom icon for your application on macOS, you should add the following line of code to your project (.pro) file:

ICON = myapplication.icns

Note that the icon format is different for Windows and macOS. For Linux distributions, there is a different approach to making the desktop entry for each flavor.

In this section, we discussed some of the platform-specific settings. In the next section, we will learn about the use of Visual Studio with Qt VS Tools.

Using Qt with Microsoft Visual Studio

Some developers choose Visual Studio as their preferred IDE. So, if your favorite IDE is Visual Studio, then you can integrate Qt VS Tools with Microsoft Visual Studio. This will allow you to use the standard Windows development environment without having to worry about Qt-related build steps or tools. You can install and update Qt VS Tools directly from Microsoft Visual Studio.

You can find Qt Visual Studio Tools from Visual Studio Marketplace for the corresponding versions. For Visual Studio 2019, you can download the tool from the following link: https://marketplace.visualstudio.com/items?itemName=TheQtCompany.QtVisualStudioTools2019. You can also download the VS add-in from the following Qt download link: https://download.qt.io/official_releases/vsaddin/.

These are some of the important features of Qt VS Tools:

  • Wizards to create new projects and classes
  • Automated build setup for moc, uic, and rcc compilers
  • Import and export of .pro and .pri files
  • Automatic conversion of a Qt VS Tools project to a qmake project
  • Integrated Qt resource management
  • Ability to create Qt translation files and integration with Qt Linguist
  • Integrated Qt Designer
  • Integrated Qt documentation
  • Debugging extensions for Qt data types

To start using the features in the Visual Studio environment, you must set the Qt version. Select the appropriate version from Options and restart the IDE. Visual Studio and Qt use different file formats to save projects. You may use .pro files with qmake or .vcproj files with Visual Studio to build your project. Since Visual Studio is used for Windows-specific development, it is recommended to use Qt Creator as the IDE for cross-platform development.

If you don't have a .vcproj file, then you can generate one from a .pro file through the command line or through VS Tools. We have already discussed the command-line instruction in the Building with qmake section. You can also convert your .pro file to a .vcproj file by using the Open option in VS Tools. Please note that the generated .vcproj file only contains Windows-specific settings.

In this section, we discussed the VS add-in. In the next section, we will learn how to run a sample application on Linux. We will skip a discussion on building and running a Qt application on Windows as we have already discussed this in earlier chapters.

Running a Qt application on Linux

Building and running a Qt application on Linux is similar to running it on Windows, but Linux has many distributions and thus it is difficult to build an application that flawlessly runs on all Linux variants. In most distributions, the application will run smoothly. We will focus on Ubuntu 20.04 as our target platform. When you install Qt on Ubuntu, it will automatically detect the kit and configurations automatically. You can also configure a kit with the appropriate compiler and Qt version, as illustrated in the following screenshot:

Figure 5.4 – Desktop kit configuration on Ubuntu

Figure 5.4 – Desktop kit configuration on Ubuntu

Let's run our HelloWorld example on Ubuntu. Hit the Run button on the left-side pane. A UI showing Hello World! will appear in no time, as illustrated in the following screenshot:

Figure 5.5 – Application running on Ubuntu

Figure 5.5 – Application running on Ubuntu

You can also run the application from the command line, as shown in the following code snippet:

$./HelloWorld

In this section, we discussed how to run our application on Linux distributions. In the next section, we will learn about running a Qt application on macOS and iOS.

Running a Qt application on macOS and iOS

We have already discussed how to build and run applications on Windows and Linux platforms in earlier chapters. Let's move on to learn how to run our applications on platforms such as macOS and iOS. To build a Qt application on macOS and iOS, you will need Xcode from the App Store. Xcode is the IDE for macOS, comprising a suite of software development tools for developing applications in macOS and iOS. If you have already installed Xcode, Qt Creator will detect its existence and will automatically detect the suitable kits. As for the kit selection, Qt for macOS supports kits for Android, clang 64-bit, iOS, and iOS Simulator.

You can see a sample desktop kit configuration on macOS in the following screenshot:

Figure 5.6 – Desktop kit configuration on macOS

Figure 5.6 – Desktop kit configuration on macOS

You can also manually add a debugger in the Debuggers tab if you don't want to use the Auto-detected debugger, as illustrated in the following screenshot:

Figure 5.7 – Debugger option on macOS

Figure 5.7 – Debugger option on macOS

Running an application on macOS is similar to running it on Windows. Just hit the Run button and you will see the application running in no time.

Mobile platforms hold equal importance to desktop platforms such as Windows, Linux, and macOS. Let's explore how to set up an environment for running applications on iOS.

Configuring Qt Creator for iOS

Running Qt applications on iOS is really simple. You can connect your iOS device and select a suitable device type from the device selection list. You can select Device type from the Kits selection screen. You can also run the application on iOS Simulator, as illustrated in the following screenshot:

Figure 5.8 – iOS Simulator option on macOS

Figure 5.8 – iOS Simulator option on macOS

After configuring the kit, just plug in your iPhone and hit the Run button. You can see a sample output in the following screenshot:

Figure 5.9 – Qt Creator running an application on an iPhone

Figure 5.9 – Qt Creator running an application on an iPhone

It is relatively easy to build and run an application on the iOS platform. However, distributing the application is not easy as the App Store is a very closed ecosystem. You should have an Apple ID and will need to sign in your iOS applications before you can distribute them to your users. You can't avoid these steps, but let's skip the deployment part for now.

You can learn more about App Store submissions at the following link:

https://developer.apple.com/app-store/submissions

In this section, we learned about running an application on macOS and iOS. In the next section, we will learn how to configure and build an application for the Android platform.

Configuring Qt Creator for Android

Android is the most popular mobile platform today, hence developers want to build applications for Android. Although Android is a Linux-based operating system, it is very different from other Linux distributions. In order to use it, you have to configure Qt Creator and install certain packages.

For smooth functioning of your Qt Creator configuration for Android, use OpenJDK 8, NDK r21 with clang toolchain. You can run sdkmanager from the ANDROID_SDK_ROOTcmdline-toolslatestin with required arguments to configure with required dependencies.

You can learn more about android specific requirements and instructions in the following link:

https://doc.qt.io/qt-6/android-getting-started.html

Let's get started with configuring your machine for Android by following these next steps:

  1. To build a Qt application on Android, you have to install the Android software development kit (SDK), the Android native development kit (NDK), the Java Development Kit (JDK), and OpenSSL to your development PC, irrespective of your desktop platform. You will find the download option with a globe icon or Download button next to each corresponding field, to download from the respective package's page.
  2. After all the required packages are installed, restart Qt Creator. Qt Creator should be able to detect the build and platform tools automatically.
  3. However, you may have to configure further to fix errors in Android settings. You may find the SDK manager, the platform SDK, and essential packages missing, as shown in the following screenshot:
    Figure 5.10 – Android Settings screen

    Figure 5.10 – Android Settings screen

  4. Select the correct SDK and NDK path under Android Settings. Click on the Apply button to save the changes.
  5. Click on the SDK Manager tab and click on the Update Installed button. You may see a message box prompting you to install missing packages, as illustrated in the following screenshot. Click on the Yes button to install the packages:
    Figure 5.11 – Information message showing missing Android packages

    Figure 5.11 – Information message showing missing Android packages

  6. You may get another message warning of Android SDK changes, listing missing essential packages, as illustrated in the following screenshot. Click on the OK button:
    Figure 5.12 – Warning about missing Android packages

    Figure 5.12 – Warning about missing Android packages

  7. Click on the Advanced Options... button to launch the SDK Manager Arguments screen, type –-verbose, and click on the OK button. You can see an overview of this in the following screenshot:
    Figure 5.13 – Android SDK Manager tool

    Figure 5.13 – Android SDK Manager tool

  8. Once the issues are resolved, you will see that all Android settings have been properly configured, as shown in the following screenshot:
    Figure 5.14 – Proper Android configuration in Qt Creator

    Figure 5.14 – Proper Android configuration in Qt Creator

  9. If the issues are still not resolved or if you want to install a specific platform, you can enter the appropriate command, as shown in the following screenshot. You may also install the required packages from the command line. Qt will automatically detect the build tools and platforms available in the SDK location:
    Figure 5.15 – Android SDK Manager tool

    Figure 5.15 – Android SDK Manager tool

  10. Once the Android settings are properly configured, you can see the Android kit is ready for development, as illustrated in the following screenshot:
    Figure 5.16 – Properly configured Android kit

    Figure 5.16 – Properly configured Android kit

  11. Select an Android kit from the Kit selection option, as illustrated in the following screenshot:
    Figure 5.17 – Android Kit selection option

    Figure 5.17 – Android Kit selection option

  12. In this step, you can select a target Android version and configure your Android application by creating a AndroidManifest.xml file with Qt Creator. You can set the package name, version code, SDK version, application icon, permissions, and so on. The settings can be seen in the following screenshot:
    Figure 5.18 – Android manifest option in build settings

    Figure 5.18 – Android manifest option in build settings

  13. Your machine is now ready for Android development. However, your Android hardware requires developer options to be enabled or the Android emulator to be used. To enable the Developer mode, go to Settings, tap on System, and then on About phone.
  14. Then, tap on Software info and find the build number. Keep tapping Builder number until you see Developer mode activated. It may take seven taps to activate the Developer mode. Now, go back to the Settings pane, where you will now find Developer options as an entry.
  15. Your Android device is ready to run the Android application. Click on the Run button and select a device from the Compatible device list screen.
  16. Next, tap Allow on the Allow USB Debugging prompt on the Android device. You will see the Hello World! message running on your Android device. You can find the .apk file generated inside the build folder.

Congratulations! You have successfully developed your Android application. Unlike iOS, Android is an open system. You can copy or distribute the .apk file into other Android devices running on the same Android version, and then install it. However, if you want to distribute your apps on Google Play Store, then you will have to register as a Google Play developer and sign the package.

In this section, we learned how to configure and build for an Android platform. In the next section, we will discuss other platforms supported by Qt 6 at the time this book was authored.

Other Qt-supported platforms

Qt 5 had support for a great range of platforms, from desktop and mobile platforms to embedded and web platforms. Qt 6 is yet to support all platforms that were supported in Qt 5, but the platforms will be gradually supported as Qt 6 matures. Currently, only embedded Linux is supported in the latest release of Qt 6 under the commercial license. You may have to wait some time to port your application to Qt 6 on a different embedded platform. Otherwise, if you want to migrate to Qt 6 immediately for your favorite embedded platform, you have to build from the source code and do the necessary modifications.

The following link provides a snapshot of embedded Linux support in Qt 6.2: https://doc-snapshots.qt.io/qt6-dev/embedded-linux.html. This link may get updated as Qt moves to the next release.

Qt also provides a Boot to Qt software stack for embedded Linux systems under commercial licenses. It is a lightweight, Qt-optimized complete software stack that is installed on the target system. The conventional embedded Linux kernel is used in the Boot to Qt software stack, which is designed with the Poky and Yocto packages.

Explore more about Boot to Qt at the following link:

https://doc.qt.io/QtForDeviceCreation/b2qt-index.html

Qt for WebAssembly allows you to build Qt applications for web platforms. It does not necessarily require any client-side installations, and saves server resources. It is a platform plugin that lets you build Qt applications that can be embedded into web pages. It is not yet available to open source developers in Qt 6. Commercial license holders may get early access to use this plugin.

You can learn more about the Qt for WebAssembly plugin at the following link:

https://wiki.qt.io/Qt_for_WebAssembly

In this section, we learned about other platforms supported in Qt 6. In the next section, we will discuss how to port your application from Qt 5 to Qt 6.

Porting from Qt 5 into Qt 6

Qt 6 is a major change to the Qt framework, therefore it breaks some of the backward compatibility. So, before upgrading to Qt 6, make sure that your Qt 5 application is updated to Qt 5.15. Porting will be easier from Qt 5.15 to Qt 6, with the fewest number of changes. However, APIs marked as deprecated or obsolete in Qt 5.15 may have been removed from Qt 6.0.

The CMake APIs in Qt 5 and Qt 6 are almost identical in terms of semantics. As a result, Qt 5.15 introduced versionless targets and commands, allowing CMake code to be written that is completely independent of Qt versions. Versionless imported targets are most useful for projects that require both Qt 5 and Qt 6 compilation. It is not recommended to use them by default because of the missing target properties. You can read more on this at the following link: https://doc.qt.io/qt-6/cmake-qt5-and-qt6-compatibility.html.

Some of the classes and modules have been removed in Qt 6, but these are kept in Qt5Compat for ease of porting. Apart from build system changes, you may need to fix up the includes directives of obsolete classes—for example, classes such as QLinkedList, QRegExp, and QTextCodec are replaced in Qt6 with new classes. But for ease of porting, you need to add core5compat into your .pro file, as shown here:

QT += core5compat

There are also changes with respect to the drawing mechanism. If you were using OpenGL-style OpenGL Shading Language (GLSL) in your project, then you would have to switch to Vulkan-style GLSL. As per new changes, you can write shaders in Vulkan-compatible GLSL and use the qsb tool. Your shader code should be compiled into Standard Portable Intermediate Representation-Vulkan (SPIR-V) format. We will discuss graphics in detail in Chapter 8, Graphics and Animations. Further details can be found at the following link: https://doc.qt.io/qt-6/qtshadertools-index.html.

There are also some changes to Qt Modeling Language (QML). The Qt Quick Extras module has merged with Qt Quick Controls. Modules such as QtGraphicalEffects have been removed from Qt 6 and will be available with a different license. Qt Quick MultiEffect is available in the Qt Marketplace and provides better performance. You might also consider updating your earlier signal connections in QML to use a JavaScript function declaration, as shown in the following code snippet:

Connections {

    target: targetElement

    function onSignalName() {//Do Something}

}

The Qt State Machine module is largely source-compatible with the Qt 5 version, so you should be able to continue working on their projects with no—or only slight—changes. To use the State Machine module's classes, add the following line of code to your Qt project (.pro) file:

QT += statemachine

To import the State Machine module inside the QML file, use the following import statement:

import QtQml.StateMachine

Qt provides detailed porting guidelines. Have a look at the following documentation if you are looking to port your Qt 5 applications to Qt 6:

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

https://www.qt.io/blog/porting-from-qt-5-to-qt-6-using-qt5compat-library

https://doc.qt.io/qt-6/porting-to-qt6-using-clazy.html

In this section, you learned how to port your application from Qt 5 to Qt 6. In the next section, we will summarize what we learned in this chapter.

Summary

This chapter explained cross-platform development using Qt Creator. You learned about various compilers, build tools, and build- and platform-specific settings. In this chapter, you learned to configure and build applications on desktop and mobile platforms and how to run applications on iPhone and Android devices. We discussed how to port your Qt project to different platforms without too many challenges.

In the next chapter, you will learn about the signal and slots mechanism, the Qt meta object system, and event handling. Let's continue!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.239.123