You are on page 1of 10

Creating a Mobile Application with Nokia

Qt SDK
Note: To complete this tutorial, you must install Nokia Qt SDK. The installation program
installs and configures the necessary tool chains for mobile application development.

This tutorial describes how to use Qt Creator to create a small Qt application, Battery
Indicator, that uses the System Information Mobility API to fetch battery information from
the device.

Creating the Battery Indicator Project


Note: Create the project with the Help mode active so that you can follow these instructions
while you work.

1. Select File > New File or Project > Qt Application Project > Mobile Qt
Application > Choose.
The Introduction and Project Location dialog opens.
2. In the Name field, type BatteryIndicator.
3. In the Create in field, enter the path for the project files. For example,
C:\Qt\examples, and then click Next.

The Select Required Qt Versions dialog opens.


4. Select Maemo, Qt Simulator, and Symbian Device targets, and click Next.

Note: Targets are listed if you installed the appropriate development environment, for
example, as part of the Nokia Qt SDK.

The Class Information dialog opens.

5. In the Class Name field, type BatteryIndicator as the class name.


6. In the Base Class list, select QDialog as the base class type.

Note: The Header File, Source File and Form File fields are automatically updated
to match the name of the class.

7. Click Next.

The Project Management dialog opens.


8. Review the project settings, and click Finish to create the project.

The BatteryIndicator project now contains the following files:

 batteryindicator.h
 batteryindicator.cpp
 main.cpp
 batteryindicator.ui
 BatteryIndicator.pro

The files come with the necessary boiler plate code that you must modify, as described in the
following sections. You do not need to change the main.cpp file.
Declaring the Qt Mobility API
The New wizard automatically adds information to the .pro file that you need when you use
the Qt Mobility APIs or develop applications for Symbian devices. You must modify the
information to declare the Qt Mobility APIs that you use.

This example uses the System Info API, so you must declare it, as illustrated by the following
code snippet:

CONFIG += mobility
MOBILITY = systeminfo

Each Mobility API has its corresponding value that you have to add as a value of MOBILITY
to use the API. For a list of the APIs and the corresponding values that you can assign to
MOBILITY, see the Quickstart Example.

The following code snippet shows information that is needed for applications developed for
Symbian device. Qt Creator generated the UID for testing the application on a device. You
only need to change the UID and capabilities if you deliver the application for public use and
need to have it Symbian Signed.

symbian {
TARGET.UID3 = 0xecbd72d7
# TARGET.CAPABILITY +=
TARGET.EPOCSTACKSIZE = 0x14000
TARGET.EPOCHEAPSIZE = 0x020000 0x800000
}

Designing the User Interface


1. In the Editor mode, double-click the batteryindicator.ui file in the Projects view to
launch the integrated Qt Designer.
2. Drag and drop a Progress Bar (QProgressBar) widget to the form.
3. In the Properties pane, change the objectName to batteryLevelBar.

Completing the Header File


The batteryindicator.h file contains some of the necessary #includes, a constructor, a
destructor, and the Ui object. You must include the System Info header file, add a shortcut to
the mobility name space, and add a private function to update the battery level value in the
indicator when the battery power level changes.

1. In the Projects view, double-click the batteryindicator.h file to open it for


editing.
2. Include the System Info header file, as illustrated by the following code snippet:

#include <QSystemInfo>

3. Add a shortcut to the mobility name space, as illustrated by the following code
snippet:

QTM_USE_NAMESPACE
4. Declare a private function in the private section, after the Ui::BatteryIndicator
function, as illustrated by the following code snippet:
5. private:
6. Ui::BatteryIndicator *ui;
7. void setupGeneral();
8.
QSystemDeviceInfo *deviceInfo;

Completing the Source File


Now that the header file is complete, move on to the source file, batteryindicator.cpp.

1. In the Projects view, double-click the batteryindicator.cpp file to open it for editing.
2. Create a QSystemDeviceInfo object and set its value. Then connect the signal that
indicates that battery level changed to the setValue slot of the progress bar. This is
illustrated by the following code snippet:
3. void BatteryIndicator::setupGeneral()
4. {
5. deviceInfo = new QSystemDeviceInfo(this);
6.
7. ui->batteryLevelBar->setValue(deviceInfo->batteryLevel());
8.
9. connect(deviceInfo, SIGNAL(batteryLevelChanged(int)),
10. ui->batteryLevelBar, SLOT(setValue(int)));
}

11. Use the constructor to set initial values and make sure that the created object is in a
defined state, as illustrated by the following code snippet:
12. BatteryIndicator::BatteryIndicator(QWidget *parent) :
13. QDialog(parent),
14. ui(new Ui::BatteryIndicator),
15. deviceInfo(NULL)
16. {
17. ui->setupUi(this);
18. setupGeneral();
}

Compiling and Running Your Program


Now that you have all the necessary code, select Qt Simulator as the target and click the
button to build your program and run it in the Qt Simulator.

In Qt Simulator, run the runOutOfBattery.qs example script to see the value change in the
Battery Indicator application. Select Scripting > examples > runOutOfBattery.qs > Run.
Testing on a Symbian Device
You also need to test the application on real devices. Before you can start testing on Symbian
devices, you must connect them to the development PC by using an USB cable and install the
necessary software on them.

1. Install Qt 4.6.2 libraries, the Qt mobile libraries, and the TRK debugging application
on the device. For more information, see Setting Up Development Environment for
Symbian.
2. Start TRK on the device.
3. Click the Target Selector and select Symbian Device.
4. Click Run to build the application for the Symbian device.

Testing on the Maemo Emulator


The Maemo emulator emulates the Nokia N900 device environment. You can test
applications in conditions practically identical to running the application on a Nokia N900
device with the software update release 1.2 (V10.2010.19-1).

For more information, see Using the Maemo Emulator.

You might also like