2

I am working on an android app in Qt and c++. My whole application has portrait orientation.But when i play a video i want to change the orientation to landscape, and after video ends it should again change to portrait.

So the question is How is it possible to set the screen to landscape or portrait modes in a Qt/C++ application for Android.

Nejat
  • 31,784
  • 12
  • 106
  • 138
Eljay
  • 941
  • 5
  • 15
  • 30

2 Answers2

6

You don't need to call it through Java code. You can directly call it from C++ using JNI as follow:

void MyAndroidHelperClass::setScreenOrientation(int orientation)
{
    QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
    if ( activity.isValid() )
    {
        activity.callMethod<void>
                ("setRequestedOrientation" // method name
                 , "(I)V" // signature
                 , orientation);
    }
}
S.M.Mousavi
  • 5,013
  • 7
  • 44
  • 59
1

Screen orientation on Android can be changed using setRequestedOrientation Java function so you should call a Java function from your app. To run Java code in your Qt Android application you should use the Qt Android Extras module which contains additional functionality for development on Android.

You can use JNI to call a Java function from C/C++ or callback a C/C++ function from Java.

Here you could have it in a static Java method like :

package com.MyApp;

public class OrientationChanger
{
    public static int change(int n)
    {
        switch(n)
        {
               case 0:
                   setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                   break;
               case 1:
                   setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                   break;                   
           }
    }
}

First you need to add this to your .pro file :

QT += androidextras

And Include the relevant header file :

#include <QAndroidJniObject>

You can then call this static Java function from your C++ code.

To change the orientation to landscape mode :

bool retVal = QAndroidJniObject::callStaticMethod<jint>
                        ("com/MyApp/OrientationChanger" // class name
                        , "change" // method name
                        , "(I)I" // signature
                        , 0);

To change the orientation to portrait mode :

bool retVal = QAndroidJniObject::callStaticMethod<jint>
                        ("com/MyApp/OrientationChanger" // class name
                        , "change" // method name
                        , "(I)I" // signature
                        , 1);
Eljay
  • 941
  • 5
  • 15
  • 30
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • Can i change orientation of android screen if it is fixed in androidManifest.xml to a particular orientation – Eljay Mar 14 '15 at 05:10
  • I am not sure of that. You can try it. Were you able to rotate the screen using the approach in the answer? – Nejat Mar 14 '15 at 05:15
  • I am going to try it now so i asked it because i have fixed orientation to portrait in android manifest – Eljay Mar 14 '15 at 05:21
  • I am trying above java code for changing orientation in android in qt.i have also imported import android.content.pm.ActivityInfo; but i am getting source not found error in setRequestedOrientation – Eljay Mar 14 '15 at 08:17
  • Have you placed your Java file in Android Package Source Directory? – Nejat Mar 14 '15 at 10:22
  • Don't you have to retrieve the current activity first to use "setRequestedOrientation"? I tried that but I always get a cannot find symbol error. – Solidus Jun 17 '15 at 16:34