Setting up Qt for mobile applications

In this example, we will learn how to set up our Qt project in Qt Quick and enable it to be build and exported to mobile devices.

How to do it…

  1. First of all, let's create a new project by going to File | New File or New Project. Then, a window will pop up for you to choose a project template. Select Qt Quick Application and click the Choose button:
    How to do it…
  2. After that, insert the project name and select the project location. Click the Next button and it will ask you to select the minimum Qt version required for your project. Please make sure that you select a version that exists on your computer, otherwise you won't be able to run it properly. Once you have done that, proceed by clicking the Next button.
  3. Then, Qt Creator will ask you which kit you want to use for your project. These "kits" are basically different compilers that you can use to compile your project for different platforms. Since we're doing an application for a mobile platform, we will enable the Android kit (or the iOS kit if you're running a Mac) in order to build and export your app to your mobile device. Do note that you need to configure the Android kit if you're using it for the first time, so that Qt can find the directory of the Android SDK. Click Next once you're done with it:
    How to do it…
  4. Once the project has been created, Qt Creator will automatically open up a file from your project, called main.qml. You will see something like this on screen, which is very different from your usual C/C++ project:
    import QtQuick 2.3
    import QtQuick.Window 2.2
    
    Window {
      visible: true
    
      MouseArea {
        anchors.fill: parent
        onClicked: {
          Qt.quit();
        }
      }
    
      Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
      }
    }
  5. Build and run the project now by clicking on the green arrow button located at the bottom-left corner of your Qt Creator. If you set the default kit to Desktop, a window will pop up which looks something like this:
    How to do it…
  6. We can switch between different kits by going to the Projects interface and selecting the kit you want your project to be built with. You can also manage all the kits available on your computer, or add a new kit to your project from the Projects interface:
    How to do it…
  7. If this is your first time building and running your project, you need to create a template for the Android kit under the Build settings. Once you have clicked the Create Templates button, Qt will generate all the files required to run your app on an Android device. If you don't plan to use Gradle in your project, disable the option Copy the Gradles files to Android directory. Otherwise, you may encounter problems when trying to compile and deploy your app to your mobile device:
    How to do it…
  8. Once you have created the template, press the Run button and now you should see a window popping up, asking which device it should export to:
    How to do it…
  9. Select the device that is currently connected to your computer and press the OK button. Wait for a while for it to build the project, and you should see something like this on your mobile device:
    How to do it…

How it works…

A Qt Quick application project is quite different from a form application project. You will be writing QML script most of the time instead of writing C/C++ code.

The Android Software Development Kit (SDK), Android Native Development Kit (NDK), Java Development Kit (JDK), and Apache Ant are required to build and export your app to the Android platform. Alternatively, you can also use Gradle instead of Apache Ant for your Android kit. All you need to do is to enable the Use Gradle instead of Ant option and provide Qt with Gradle's installation path. Note that Android Studio is currently not supported by Qt Creator:

How it works…

If you're running the app on an Android device, make sure that you have enabled USB Debugging Mode. To enable USB Debugging Mode, you need to first enable the developer options on your Android device by going to Settings | About Phone and tap the Build Number seven times. After that, go to Settings | Developer Options and you will see the Android Debugging option in the menu. Enable that option and you can now export your app to your device for testing.

To build for the iOS platform, you need to run Qt Creator on a Mac and make sure the latest XCode is installed on your Mac as well.

To test your app on an iOS device, you need to register a developer account with Apple, register your device at the developer portal, and install the provisioning to your XCode, which is a lot trickier than Android. You will be given access to the developer portal once you have obtained a developer account from Apple.

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

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