Kamis, 10 Mei 2012

Creating a Mobile Application


Creating a Mobile Application with Qt SDK

Note: To complete this tutorial, you must install 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.
Select File > New File or Project > Qt C++ Project > Mobile Qt Application > Choose.


The Introduction and Project Location dialog opens.


  1. In the Name field, type BatteryIndicator.
  2. In the Create in field, enter the path for the project files. For example, C:\Qt\examples, and then click Next.

The Qt Versions dialog opens.


  1. Select Maemo, Qt Simulator, and Symbian Device targets, and click Next.
  2. Note: Targets are listed if you installed the appropriate development environment, for example, as part of the Qt SDK. You can add targets later in theProjects mode.

The Application Options dialog opens.


  1. In the Orientation behavior field, determine how the application behaves when the orientation of the device display rotates between portrait and landscape, and then click Next.
  2. Note: Qt Creator contains a default program icon and generates a UID for testing the application on a device. You only need to specify the Symbian Specific and Maemo Specific settings if you deliver the application for public use.

The Project Management dialog opens.

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





The BatteryIndicator project now contains the following files:
  • BatteryIndicator.pro
  • main.cpp
  • BatteryIndicator.svg
  • BatteryIndicator.png
  • BatteryIndicator.desktop
  • deployment.pri
  • mainwindow.cpp
  • mainwindow.ui
  • mainwindow.h
templates for debian deployment files



The files come with the necessary boiler plate code that you must modify, as described in the following sections.
Declaring the Qt Mobility API

To use the Qt Mobility APIs or develop applications for Symbian devices, you must modify the .pro file 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.

Designing the User Interface

  1. In the Editor mode, double-click the mainwindow.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.
  4. Right-click the MainWindow object and select Lay Out > Lay Out Horizontally to ensure that the battery indicator widget size is adjusted correctly on Maemo devices.

Completing the Header File

The mainwindow.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 mainwindow.h file to open it for editing.
  2. Include the System Device Info header file, as illustrated by the following code snippet:

  1.  #include <QSystemDeviceInfo>
  2. Add a shortcut to the mobility name space, as illustrated by the following code snippet:
     QTM_USE_NAMESPACE
  3. Declare a private function in the private section, after the Ui::MainWindow function, as illustrated by the following code snippet:
     private:
         Ui::MainWindow *ui;
         void setupGeneral();
    
         QSystemDeviceInfo *deviceInfo;

Completing the Source File

Now that the header file is complete, move on to the source file, mainwindow.cpp.
  1. In the Projects view, double-click the mainwindow.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:
     void MainWindow::setupGeneral()
     {
         deviceInfo = new QSystemDeviceInfo(this);
    
         ui->batteryLevelBar->setValue(deviceInfo->batteryLevel());
    
         connect(deviceInfo, SIGNAL(batteryLevelChanged(int)),
                 ui->batteryLevelBar, SLOT(setValue(int)));
     }
  3. 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:
     MainWindow::MainWindow(QWidget *parent):
         QMainWindow(parent),
         ui(new Ui::MainWindow),
         deviceInfo(NULL)
     {
         ui->setupUi(this);
         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.
"Mobile example in Qt Simulator"

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.
Install Qt libraries, Qt mobile libraries, and the TRK debugging application on the device. For more information, see Setting Up Development Environment for Symbian.
Start TRK on the device.
Click the Target Selector and select Symbian Device.
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.3 (V20.2010.36-2).

Tidak ada komentar:

Posting Komentar

masukkan komentar anda disini