How to Create and Run Qt Application for Android: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:Developing_with_Qt::Ports::Android]]<br />[[Category:HowTo]]<br />[[Category:Tutorial]]
[[Category:Developing_with_Qt::Ports::Android]]
[[Category:HowTo]]
[[Category:Tutorial]]


[toc align_right=&quot;yes&amp;quot; depth=&quot;2&amp;quot;]
[toc align_right="yes" depth="2"]


'''English''' [[How_to_Create_and_Run_Qt_Application_for_Android_Bulgarian|Български]]
'''English''' [[How_to_Create_and_Run_Qt_Application_for_Android_Bulgarian|Български]]
Line 7: Line 9:
= How to Create and Run Qt Application for Android =
= How to Create and Run Qt Application for Android =


Using &quot;Necessitas&amp;quot;:http://developer.qt.nokia.com/wiki/Necessitas 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.
Using "Necessitas":http://developer.qt.nokia.com/wiki/Necessitas 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 ==
== Requirements ==
Line 17: Line 19:


# Start Qt Creator from Necessitas Qt SDK
# Start Qt Creator from Necessitas Qt SDK
# Press '''File &gt; New file or project…''' and follow the instructions of the wizard
# Press '''File > New file or project…''' and follow the instructions of the wizard
# Select '''Qt Widget Project''' and '''Qt Gui Application'''
# Select '''Qt Widget Project''' and '''Qt Gui Application'''
# Enter project name and path to its directory
# Enter project name and path to its directory
Line 26: Line 28:
=== Hints ===
=== Hints ===


* To modify general information about the application, Android Manifest file, Android Permissions or the list of libraries from Qt Creator go to '''Projects &gt; Run Settings &gt; Package configuration &gt; Details'''.
* 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 &gt; Run Settings &gt; Package configuration &gt; Details &gt; Manifest'''.
* 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 &gt; Run Settings &gt; Configuration configuration &gt; Details'''.
* To modify deploy configuration of the project from Qt Creator go to '''Projects > Run Settings > Configuration configuration > Details'''.


== Source Code ==
== Source Code ==
Line 39: Line 41:


<code>QT ''= core gui
<code>QT ''= core gui
<br />TARGET = test<br />TEMPLATE = app


<br />SOURCES''= main.cpp mainwindow.cpp
TARGET = test
TEMPLATE = app
 
 
SOURCES''= main.cpp mainwindow.cpp


HEADERS ''= mainwindow.h
HEADERS ''= mainwindow.h
<br />FORMS''= mainwindow.ui


CONFIG ''= mobility<br />MOBILITY =
FORMS''= mainwindow.ui
<br />OTHER_FILES''=  android/AndroidManifest.xml  android/res/drawable-hdpi/icon.png  android/res/drawable-ldpi/icon.png  android/res/drawable-mdpi/icon.png  android/res/values/libs.xml  android/res/values/strings.xml  android/src/eu/licentia/necessitas/industrius/QtActivity.java  android/src/eu/licentia/necessitas/industrius/QtApplication.java  android/src/eu/licentia/necessitas/industrius/QtLayout.java  android/src/eu/licentia/necessitas/industrius/QtSurface.java  android/src/eu/licentia/necessitas/ministro/IMinistro.aidl  android/src/eu/licentia/necessitas/ministro/IMinistroCallback.aidl  android/src/eu/licentia/necessitas/mobile/QtAndroidContacts.java  android/src/eu/licentia/necessitas/mobile/QtCamera.java  android/src/eu/licentia/necessitas/mobile/QtFeedback.java  android/src/eu/licentia/necessitas/mobile/QtLocation.java  android/src/eu/licentia/necessitas/mobile/QtMediaPlayer.java  android/src/eu/licentia/necessitas/mobile/QtSensors.java  android/src/eu/licentia/necessitas/mobile/QtSystemInfo.java</code>
 
CONFIG ''= mobility
MOBILITY =
 
OTHER_FILES''=  android/AndroidManifest.xml  android/res/drawable-hdpi/icon.png  android/res/drawable-ldpi/icon.png  android/res/drawable-mdpi/icon.png  android/res/values/libs.xml  android/res/values/strings.xml  android/src/eu/licentia/necessitas/industrius/QtActivity.java  android/src/eu/licentia/necessitas/industrius/QtApplication.java  android/src/eu/licentia/necessitas/industrius/QtLayout.java  android/src/eu/licentia/necessitas/industrius/QtSurface.java  android/src/eu/licentia/necessitas/ministro/IMinistro.aidl  android/src/eu/licentia/necessitas/ministro/IMinistroCallback.aidl  android/src/eu/licentia/necessitas/mobile/QtAndroidContacts.java  android/src/eu/licentia/necessitas/mobile/QtCamera.java  android/src/eu/licentia/necessitas/mobile/QtFeedback.java  android/src/eu/licentia/necessitas/mobile/QtLocation.java  android/src/eu/licentia/necessitas/mobile/QtMediaPlayer.java  android/src/eu/licentia/necessitas/mobile/QtSensors.java  android/src/eu/licentia/necessitas/mobile/QtSystemInfo.java</code>


* main.cpp
* main.cpp


<code>#include &lt;QtGui/QApplication&amp;gt;<br />#include &quot;mainwindow.h&amp;quot;
<code>#include <QtGui/QApplication>
#include "mainwindow.h"
 
int main(int argc, char '''argv[])
{
QApplication a(argc, argv);
MainWindow w;
#if defined(Q_WS_S60)
w.showMaximized();
#else
w.show();
#endif
 
return a.exec();
}</code>
 
''' mainwindow.h


int main(int argc, char '''argv[])<br />{<br /> QApplication a(argc, argv);<br /> MainWindow w;<br />#if defined(Q_WS_S60)<br /> w.showMaximized();<br />#else<br /> w.show();<br />#endif
<code>#ifndef MAINWINDOW_H
<br /> return a.exec&amp;amp;#40;&amp;#41;;<br />}</code>
#define MAINWINDOW_H
<br />''' mainwindow.h


<code>#ifndef MAINWINDOW_H<br />#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>


#include &lt;QMainWindow&amp;gt;<br />#include &lt;QLabel&amp;gt;
namespace Ui {
class MainWindow;
}


namespace Ui {<br /> class MainWindow;<br />}
class MainWindow : public QMainWindow
{
Q_OBJECT


class MainWindow : public QMainWindow<br />{<br /> Q_OBJECT
public:
explicit MainWindow(QWidget '''parent = 0);
~MainWindow();
 
private:


public:<br /> explicit MainWindow(QWidget '''parent = 0);<br /> ~MainWindow();
// from QMainWindow
<br />private:
void resizeEvent(QResizeEvent''' event);
<br /> // from QMainWindow<br /> void resizeEvent(QResizeEvent''' event);


private:
private:


QLabel* m_pLabel;<br />};
QLabel* m_pLabel;
};


#endif // MAINWINDOW_H</code>
#endif // MAINWINDOW_H</code>
Line 77: Line 109:
* mainwindow.cpp
* mainwindow.cpp


<code>#include &quot;mainwindow.h&amp;quot;
<code>#include "mainwindow.h"
 
#include <QApplication>
#include <QDesktopWidget>
#include <QtCore/QCoreApplication>
 
MainWindow::MainWindow(QWidget '''parent) :
QMainWindow(parent),
m_pLabel(NULL)
{
m_pLabel = new QLabel("Hello Qt for Android!", this);
m_pLabel->setGeometry(QApplication::desktop()->screenGeometry());
m_pLabel->setAlignment(Qt::AlignCenter);
m_pLabel->setStyleSheet("background-color:#006600; color:#FFFFFF");
}
 
MainWindow::~MainWindow()
{


#include &lt;QApplication&amp;gt;<br />#include &lt;QDesktopWidget&amp;gt;<br />#include &lt;QtCore/QCoreApplication&amp;gt;
}


MainWindow::MainWindow(QWidget '''parent) :<br /> QMainWindow(parent),<br /> m_pLabel(NULL)<br />{<br /> m_pLabel = new QLabel(&quot;Hello Qt for Android!&quot;, this);<br /> m_pLabel-&gt;setGeometry(QApplication::desktop()<s>&gt;screenGeometry());<br /> m_pLabel</s>&gt;setAlignment(Qt::AlignCenter);<br /> m_pLabel-&gt;setStyleSheet(&quot;background-color:#006600; color:#FFFFFF&amp;quot;);<br />}
void MainWindow::resizeEvent(QResizeEvent''' /*event*/)
<br />MainWindow::~MainWindow()<br />{
{
<br />}
m_pLabel->setGeometry(QApplication::desktop()->screenGeometry());
<br />void MainWindow::resizeEvent(QResizeEvent''' /*event*/)<br />{<br /> m_pLabel-&gt;setGeometry(QApplication::desktop()-&gt;screenGeometry());<br />}</code>
}</code>


== Testing ==
== Testing ==
Line 90: Line 139:
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.
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 &quot;LG GT540 Optimus&amp;quot;:http://www.gsmarena.com/lg_gt540_optimus-3081.php with Android 2.1.
This example application has been successfully tested on "LG GT540 Optimus":http://www.gsmarena.com/lg_gt540_optimus-3081.php with Android 2.1.


== See Also ==
== See Also ==


&quot;How to use Necessitas 0.2.1 Emulator on Windows 7&amp;quot;:http://developer.qt.nokia.com/wiki/How_to_use_Necessitas_Emulator_on_Windows_7<br />&quot;How to write Qt apps for Android&amp;quot;:http://sourceforge.net/p/necessitas/wiki/How to write Qt apps for Android/<br />&quot;Setup QtCreator&amp;quot;:http://sourceforge.net/p/necessitas/wiki/Setup QtCreator/
"How to use Necessitas 0.2.1 Emulator on Windows 7":http://developer.qt.nokia.com/wiki/How_to_use_Necessitas_Emulator_on_Windows_7
"How to write Qt apps for Android":http://sourceforge.net/p/necessitas/wiki/How to write Qt apps for Android/
"Setup QtCreator":http://sourceforge.net/p/necessitas/wiki/Setup QtCreator/

Revision as of 08:36, 25 February 2015


[toc align_right="yes" depth="2"]

English Български

How to Create and Run Qt Application for Android

Using "Necessitas":http://developer.qt.nokia.com/wiki/Necessitas 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
QT ''= core gui

TARGET = test
TEMPLATE = app


SOURCES''= main.cpp mainwindow.cpp

HEADERS ''= mainwindow.h

FORMS''= mainwindow.ui

CONFIG ''= mobility
MOBILITY =

OTHER_FILES''=  android/AndroidManifest.xml  android/res/drawable-hdpi/icon.png  android/res/drawable-ldpi/icon.png  android/res/drawable-mdpi/icon.png  android/res/values/libs.xml  android/res/values/strings.xml  android/src/eu/licentia/necessitas/industrius/QtActivity.java  android/src/eu/licentia/necessitas/industrius/QtApplication.java  android/src/eu/licentia/necessitas/industrius/QtLayout.java  android/src/eu/licentia/necessitas/industrius/QtSurface.java  android/src/eu/licentia/necessitas/ministro/IMinistro.aidl  android/src/eu/licentia/necessitas/ministro/IMinistroCallback.aidl  android/src/eu/licentia/necessitas/mobile/QtAndroidContacts.java  android/src/eu/licentia/necessitas/mobile/QtCamera.java  android/src/eu/licentia/necessitas/mobile/QtFeedback.java  android/src/eu/licentia/necessitas/mobile/QtLocation.java  android/src/eu/licentia/necessitas/mobile/QtMediaPlayer.java  android/src/eu/licentia/necessitas/mobile/QtSensors.java  android/src/eu/licentia/necessitas/mobile/QtSystemInfo.java
  • main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char '''argv[])
{
 QApplication a(argc, argv);
 MainWindow w;
#if defined(Q_WS_S60)
 w.showMaximized();
#else
 w.show();
#endif

 return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>

namespace Ui {
 class MainWindow;
}

class MainWindow : public QMainWindow
{
 Q_OBJECT

public:
 explicit MainWindow(QWidget '''parent = 0);
 ~MainWindow();

private:

 // from QMainWindow
 void resizeEvent(QResizeEvent''' event);

private:

QLabel* m_pLabel;
};

#endif // MAINWINDOW_H
  • mainwindow.cpp
#include "mainwindow.h"

#include <QApplication>
#include <QDesktopWidget>
#include <QtCore/QCoreApplication>

MainWindow::MainWindow(QWidget '''parent) :
 QMainWindow(parent),
 m_pLabel(NULL)
{
 m_pLabel = new QLabel("Hello Qt for Android!", this);
 m_pLabel->setGeometry(QApplication::desktop()->screenGeometry());
 m_pLabel->setAlignment(Qt::AlignCenter);
 m_pLabel->setStyleSheet("background-color:#006600; color:#FFFFFF");
}

MainWindow::~MainWindow()
{

}

void MainWindow::resizeEvent(QResizeEvent''' /*event*/)
{
 m_pLabel->setGeometry(QApplication::desktop()->screenGeometry());
}

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":http://www.gsmarena.com/lg_gt540_optimus-3081.php with Android 2.1.

See Also

"How to use Necessitas 0.2.1 Emulator on Windows 7":http://developer.qt.nokia.com/wiki/How_to_use_Necessitas_Emulator_on_Windows_7 "How to write Qt apps for Android":http://sourceforge.net/p/necessitas/wiki/How to write Qt apps for Android/ "Setup QtCreator":http://sourceforge.net/p/necessitas/wiki/Setup QtCreator/