Qt for Android development example tutorial

Source: Internet
Author: User

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:

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

3. Interaction between C ++ and qml js Methods

 
 
  1. QQuickView view;
  2. View. setSource (QUrl (QStringLiteral ("qrc: // qml/MainView. qml ")));
  3. QObject * qmlObj = (QObject *) view. rootObject ();
  4. MainWnd * w = new MainWnd (object );
  5. // Expose the C ++ class to qml for calling. The alias is mainWndClass.
  6. View. engine ()-> rootContext ()-> setContextProperty (QLatin1String ("mainWndClass"), w );
  7.  
  8. // C ++ calls the js method in qml
  9. // The parameter must be converted to QVariant
  10. QMetaObject: invokeMethod (qmlObj, "showRight", Q_ARG (QVariant, 1 ));
  11. // Call the subitem's js method
  12. QmlPlayer = qmlObj-> findChild <QObject *> ("playerArea ");
  13. QMetaObject: invokeMethod (qmlPlayer, "setVideoFile", Q_ARG (QVariant, currentVideoFile ));
 
 
  1. // MainView. qml
  2. Rectangle {
  3. Anchors. fill: parent
  4. Property int leftAreaWidth: this. width/5*4
  5. Property int rightAreaWidth: this. width/5
  6. Property int queueFontSize
  7. Function showRight (isShow ){
  8. ....
  9. }
  10. Player {
  11. Id: playerArea
  12. // Set objectName, which can be found in c ++
  13. ObjectName: "playerArea"
  14. Width: parent. width
  15. Height: parent. height
  16. }
  17. }

4. c ++ calls java Android api

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

 
 
  1. 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.

 
 
  1. Package org. rophie. ProjectName;
  2. Import org. qtproject. qt5.android. bindings. QtActivity;
  3. Import android. widget. Toast;
  4. Import android. media. AudioManager;
  5. Import android. content. Context;
  6. Public class JavaClass extends QtActivity {
  7. Private static JavaClass m_instance;
  8. Private static AudioManager mAudioManager;
  9. Public JavaClass ()
  10. {
  11. // The constructor is required
  12. M_instance = this;
  13. }
  14. Public static void setVolume (int vol ){
  15. If (mAudioManager = null ){
  16. MAudioManager = (AudioManager) m_instance.getSystemService (Context. AUDIO_SERVICE );
  17. }
  18. MAudioManager. setStreamVolume (AudioManager. STREAM_MUSIC, vol, 0 );
  19. }
  20. }

C ++ call:

 
 
  1. QAndroidJniObject: callStaticMethod <void> ("org/rophie/ProjectName/JavaClass", "setVolume", "(I) V", 3 );
  2.  
  3. // For details, refer to the QAndroidJniObject class.

5. BroadcastReceiver enables Automatic startup, which is exactly the same as Android

 
 
  1. Public void onReceive (Context context, Intent intent ){
  2. ......
  3. // JavaClass is the java main class that inherits QtActivity
  4. Intent intent2 = new Intent (context, JavaClass. class );
  5. ......
  6. }

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.

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.