Qt for Android Development example tutorial

  • 2020-06-07 05:16:26
  • OfStack

This article describes how to develop Android applications using Qt5.3.0. Due to the lack of official information, the problems encountered in the development process and the solutions are recorded here. The specific steps are as follows:

1. Video playing on Android platform can only use MediaPlayer of qml

2. The path of the control in qml must add file:// for example:


Image{
  source: "file:///mnt/usbhost1/Config/logo.png"
}

3. Intermodulation between C++ and js method in qml


QQuickView view;
view.setSource(QUrl(QStringLiteral("qrc:///qml/MainView.qml")));
QObject *qmlObj =(QObject*) view.rootObject();
MainWnd *w=new MainWnd(object);
// exposed C++ Class to qml For it to call, alias mainWndClass
view.engine ()->rootContext ()->setContextProperty (QLatin1String("mainWndClass"),w);

//c++ call qml In the js methods 
// The parameter must be converted to QVariant
QMetaObject::invokeMethod (qmlObj,"showRight",Q_ARG(QVariant,1));
// Calling a child js methods 
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 up the objectName In the c++ In order to find it 
    objectName: "playerArea"
    width: parent.width
    height: parent.height
  }
}

4.c++ calls java Android api

Create a directory \android\src\org\rophie\ProjectName\ JavaClass.java under the project directory


org\rophie\ProjectName Is the java The package name of a class package org.rophie.ProjectName;

So let's say I call Android API to adjust the volume of the system


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()
  {
    // Constructors 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 + + call:


QAndroidJniObject::callStaticMethod<void>("org/rophie/ProjectName/JavaClass","setVolume","(I)V",3);

 
// Specific reference to QAndroidJniObject class 

5.BroadcastReceiver implements startup and is similar to Android1 module


public void onReceive(Context context, Intent intent) {
 ......
 //JavaClass For inheritance QtActivity the java The main class 
 Intent intent2 = new Intent(context, JavaClass.class);
 ......
}

6. Call the third party jar package, create a new directory libs under src sibling directory, copy.jar into it and use it

Hopefully, the approach described in this article has been helpful in developing Android.


Related articles: