Qt for Android Development Example Tutorial _android

Source: Internet
Author: User

This article describes the use of Qt5.3.0 to develop Android applications, due to the less official information, where the development process encountered problems and solutions. The specific steps are as follows:

1.Android platform video playback, only the use of QML MediaPlayer

The path to the control in 2.QML must be file://for example:

image{
  Source: "File:///mnt/usbhost1/Config/logo.png"
}

The method of 3.c++ and QML in JS

Qquickview view;
View.setsource (Qurl) (Qstringliteral ("qrc:///qml/mainview.qml"));
QObject *qmlobj = (qobject*) view.rootobject ();
Mainwnd *w=new Mainwnd (object);
Exposes C + + classes to QML for their invocation, alias Mainwndclass
view.engine ()->rootcontext ()->setcontextproperty (qlatin1string () Mainwndclass "), W);

C + + Call QML in the JS method
//parameter must be converted to Qvariant
Qmetaobject::invokemethod (qmlobj, "Showright", Q_arg (qvariant,1));
The JS method that invokes the subkey
Qmlplayer = Qmlobj->findchild<qobject*> ("Playerarea");
Qmetaobject::invokemethod (Qmlplayer, "Setvideofile", Q_arg (Qvariant,currentvideofile));

Mainview.qml
Rectangle {
  anchors.fill:parent property
  int leftareawidth:this.width/5*4
  Property int RIGHTAREAWIDTH:THIS.WIDTH/5 Property
  int queuefontsize
  function showright (isshow) {
    ...
  }
  player{
    Id:playerarea
    //Set objectname in C + + to find it
    objectname: "Playerarea"
    width:parent.width
    height:parent.height
  }
}

4.c++ Invoke Java Android API

Create a directory under the project directory \android\src\org\rophie\projectname\javaclass.java

Org\rophie\projectname is the package name of the Java class package org.rophie.ProjectName;

If I call the Android API to adjust the system volume

Package org.rophie.ProjectName;
Import org.qtproject.qt5.android.bindings.QtActivity;
Import Android.widget.Toast;
Import Android.media.AudioManager;
Import Android.content.Context;
public class Javaclass extends qtactivity{
  private static Javaclass m_instance;
  private static Audiomanager Maudiomanager;
  Public Javaclass ()
  {
    //constructor must
    m_instance = this;
  }
   public static void SetVolume (int vol) {
   if (maudiomanager==null) {
     Maudiomanager = (audiomanager) m_ Instance.getsystemservice (Context.audio_service);
   }
     Maudiomanager.setstreamvolume (Audiomanager.stream_music, vol., 0);
   }
}

C + + Calls:

Qandroidjniobject::callstaticmethod<void> ("Org/rophie/projectname/javaclass", "SetVolume", "(I) V", 3);

 
Specific reference Qandroidjniobject class

5.BroadcastReceiver to boot, exactly the same as Android

public void OnReceive (context context, Intent Intent) {
 ...
 Javaclass is the Java main class that inherits Qtactivity
 Intent intent2 = new Intent (context, javaclass.class);
 ......
}

6. Call the third party jar package, create a new directory libs in the SRC sibling directory, and use the. jar.

It is hoped that the method described in this article will be helpful to the development of Android.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.