English Български

How to Create and Run Qt Application for Android

Using Necessitas [developer.qt.nokia.com] Qt SDK developers can deploy Qt application on Android devices. The following tutorial will show the basics steps to create a simple Qt application for Android.

Requirements

  • Installed Necessitas SDK
  • Android device

Tutorial

  1. Start Qt Creator from Necessitas Qt SDK
  2. Press File > New file or project… and follow the instructions of the wizard
  3. Select Qt Widget Project and Qt Gui Application
  4. Enter project name and path to its directory
  5. Select Android as a Target
  6. When the project is created open the Projects view and verify the configurations for Build and Run settings
  7. Modify the source code and when ready run the application on the device

Hints

  • To modify general information about the application, Android Manifest file, Android Permissions or the list of libraries from Qt Creator go to Projects > Run Settings > Package configuration > Details.
  • To select Android target SDK that you want to use from Qt Creator go to Projects > Run Settings > Package configuration > Details > Manifest.
  • To modify deploy configuration of the project from Qt Creator go to Projects > Run Settings > Configuration configuration > Details.

Source Code

As you can see the source code does not include any specific Android features which makes absolutely portable. Some additional files required by necessitas are added at pro file of the project.

  • test.pro

  1. QT       += core gui
  2.  
  3. TARGET = test
  4. TEMPLATE = app
  5.  
  6.  
  7. SOURCES += main.cpp\
  8.         mainwindow.cpp
  9.  
  10. HEADERS  += mainwindow.h
  11.  
  12. FORMS    += mainwindow.ui
  13.  
  14. CONFIG += mobility
  15. MOBILITY =
  16.  
  17. OTHER_FILES += \
  18.     android/AndroidManifest.xml \
  19.     android/res/drawable-hdpi/icon.png \
  20.     android/res/drawable-ldpi/icon.png \
  21.     android/res/drawable-mdpi/icon.png \
  22.     android/res/values/libs.xml \
  23.     android/res/values/strings.xml \
  24.     android/src/eu/licentia/necessitas/industrius/QtActivity.java \
  25.     android/src/eu/licentia/necessitas/industrius/QtApplication.java \
  26.     android/src/eu/licentia/necessitas/industrius/QtLayout.java \
  27.     android/src/eu/licentia/necessitas/industrius/QtSurface.java \
  28.     android/src/eu/licentia/necessitas/ministro/IMinistro.aidl \
  29.     android/src/eu/licentia/necessitas/ministro/IMinistroCallback.aidl \
  30.     android/src/eu/licentia/necessitas/mobile/QtAndroidContacts.java \
  31.     android/src/eu/licentia/necessitas/mobile/QtCamera.java \
  32.     android/src/eu/licentia/necessitas/mobile/QtFeedback.java \
  33.     android/src/eu/licentia/necessitas/mobile/QtLocation.java \
  34.     android/src/eu/licentia/necessitas/mobile/QtMediaPlayer.java \
  35.     android/src/eu/licentia/necessitas/mobile/QtSensors.java \
  36.     android/src/eu/licentia/necessitas/mobile/QtSystemInfo.java

  • main.cpp

  1. #include <QtGui/QApplication>
  2. #include "mainwindow.h"
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication a(argc, argv);
  7.     MainWindow w;
  8. #if defined(Q_WS_S60)
  9.     w.showMaximized();
  10. #else
  11.     w.show();
  12. #endif
  13.  
  14.     return a.exec();
  15. }

  • mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QLabel>
  6.  
  7. namespace Ui {
  8.     class MainWindow;
  9. }
  10.  
  11. class MainWindow : public QMainWindow
  12. {
  13.     Q_OBJECT
  14.  
  15. public:
  16.     explicit MainWindow(QWidget *parent = 0);
  17.     ~MainWindow();
  18.  
  19. private:
  20.  
  21.     // from QMainWindow
  22.     void resizeEvent(QResizeEvent* event);
  23.  
  24. private:
  25.  
  26.     QLabel* m_pLabel;
  27. };
  28.  
  29. #endif // MAINWINDOW_H

  • mainwindow.cpp

  1. #include "mainwindow.h"
  2.  
  3. #include <QApplication>
  4. #include <QDesktopWidget>
  5. #include <QtCore/QCoreApplication>
  6.  
  7. MainWindow::MainWindow(QWidget *parent) :
  8.     QMainWindow(parent),
  9.     m_pLabel(NULL)
  10. {
  11.     m_pLabel = new QLabel("Hello Qt for Android!", this);
  12.     m_pLabel->setGeometry(QApplication::desktop()->screenGeometry());
  13.     m_pLabel->setAlignment(Qt::AlignCenter);
  14.     m_pLabel->setStyleSheet("background-color:#006600; color:#FFFFFF");
  15. }
  16.  
  17. MainWindow::~MainWindow()
  18. {
  19.  
  20. }
  21.  
  22. void MainWindow::resizeEvent(QResizeEvent* /*event*/)
  23. {
  24.     m_pLabel->setGeometry(QApplication::desktop()->screenGeometry());
  25. }

Testing

Connect your device to the computer and run the application from Necessitas Qt SDK Qt Creator. The Qt application will automatically start on the Android device. You might be prompted to download dependencies using Ministro. Follow the instruction to install all required components.

This example application has been successfully tested on LG GT540 Optimus [gsmarena.com] with Android 2.1.

See Also

How to use Necessitas 0.2.1 Emulator on Windows 7 [developer.qt.nokia.com]
How to write Qt apps for Android [sourceforge.net] to write Qt apps for Android/
Setup QtCreator [sourceforge.net] QtCreator/
How to install Necessitas SDK [sourceforge.net] to install Necessitas SDK/

Categories: