Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

Creating a Qt Widget Based Mobile Application

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.

The user interface for the application is designed using Qt widgets. This enforces a platform look and feel for Symbian devices and Maemo 5 devices. However, to achieve a platform look and feel for MeeGo Harmattan devices, create a Qt Quick Application and use the Qt Quick components for MeeGo.

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 Widget Project > Mobile Qt Application > Choose.

    "New File or Project dialog"

    The Introduction and Project Location dialog opens.

    "Introduction and Project Location dialog"

  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 Target Setup dialog opens.

    "Target Setup dialog"

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

    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 the Projects mode.

    The Mobile Options dialog opens.

    "Mobile Options dialog"

  5. 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.

    Note: This dialog opens only if you select Maemo5 or Symbian Device target in the Target Setup dialog. On Harmattan, the Qt Quick Components for MeeGo provide native-looking rotation.

    The Symbian Specific dialog opens.

    "Symbian Specific dialog"

    Note: Qt Creator contains a default program icon and generates an Application UID, for testing the application on a device. You only need to change the icon and UID if you deliver the application for public use.

  6. Click Next.

    The Maemo Specific dialog opens.

    "Maemo Specific dialog"

  7. In the Application icon field, select the application icon to use on Maemo 5 or Harmattan targets, or click Next to use the default icon.

    The Project Management dialog opens.

    "Project Management dialog"

  8. 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

"Project contents"

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.

    "Adding widgets to the UI"

  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:
     #include <QSystemDeviceInfo>
  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::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 a USB cable and install the necessary software on them.

  1. Install Qt libraries, Qt mobile libraries, and a debugging agent on the device. For more information, see Connecting Symbian Devices.
  2. Start the CODA debugging agent 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 or MeeGo Harmattan Emulator

The Maemo 5 (Fremantle) and MeeGo Harmattan emulator are installed as part of the Qt SDK. After they are installed, you can start them from Qt Creator.

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).

The MeeGo Harmattan emulator emulates the Nokia N9 device environment.

For more information, see Using Maemo or MeeGo Harmattan Emulator.

X

Thank you for giving your feedback.

Make sure it is related to this specific page. For more general bugs and requests, please use the Qt Bug Tracker.

[0]; s.parentNode.insertBefore(ga, s); })();
Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. Microsoft ouvre aux autres compilateurs C++ AMP, la spécification pour la conception d'applications parallèles C++ utilisant le GPU 22
  2. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  3. RIM : « 13 % des développeurs ont gagné plus de 100 000 $ sur l'AppWord », Qt et open-source au menu du BlackBerry DevCon Europe 0
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
Page suivante

Le Qt Labs au hasard

Logo

La folie est de mettre en forme le même texte

Les Qt Labs sont les laboratoires des développeurs de Qt, où ils peuvent partager des impressions sur le framework, son utilisation, ce que pourrait être son futur. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt qtcreator-2.3
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web