Qt for Android development example tutorial
This article describes how to use Qt5.3.0 to develop Android applications. Due to a small amount of official information, we will record the problems encountered during the development process and solutions. The procedure is as follows:
1. Only qml MediaPlayer can be used for video playback on the Android platform.
2. file: // For example:
- Image{
- source: "file:///mnt/usbhost1/Config/logo.png"
- }
3. Interaction between C ++ and qml js Methods
- QQuickView view;
- View. setSource (QUrl (QStringLiteral ("qrc: // qml/MainView. qml ")));
- QObject * qmlObj = (QObject *) view. rootObject ();
- MainWnd * w = new MainWnd (object );
- // Expose the C ++ class to qml for calling. The alias is mainWndClass.
- View. engine ()-> rootContext ()-> setContextProperty (QLatin1String ("mainWndClass"), w );
-
- // C ++ calls the js method in qml
- // The parameter must be converted to QVariant
- QMetaObject: invokeMethod (qmlObj, "showRight", Q_ARG (QVariant, 1 ));
- // Call the subitem's js method
- 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, which can be found in c ++
- ObjectName: "playerArea"
- Width: parent. width
- Height: parent. height
- }
- }
4. c ++ calls java Android api
Create the \ android \ src \ org \ rophie \ ProjectName \ JavaClass. java directory under the project directory
- Org \ rophie \ ProjectName is the package name of the java class org. rophie. ProjectName;
For example, 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 ()
- {
- // The constructor is required
- 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 );
-
- // For details, refer to the QAndroidJniObject class.
5. BroadcastReceiver enables Automatic startup, which is 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 a third-party jar package, create a directory libs under the src directory at the same level, and import. jar packages to use
I hope this method will be helpful for Android development.