Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt android screen orientation



  • Hi,

    Is there a way to set the screen orientation in Qt 5.12.3 programmatically, and without changing the AndroidManifest.xml file ?



  • @zine Hi, it is simple to change screen orientation from Qt:

        enum Orientation
        {
            Unlocked = 4,           // SCREEN_ORIENTATION_SENSOR    (Android)
            LandscapeLocked = 0,    // SCREEN_ORIENTATION_LANDSCAPE (Android)
            PortraitLocked = 1,     // SCREEN_ORIENTATION_PORTRAIT  (Android)
        };
    
    void setOrientation(Orientation orientation)
    {
        const QAndroidJniObject Activity = QtAndroid::androidActivity();
        QAndroidJniEnvironment JniEnv;
    
        Activity.callMethod<void>("setRequestedOrientation", "(I)V", orientation);
        if(JniEnv->ExceptionCheck())
        {
            qDebug() << "setOrientation:: failed to set orientation!!!";
            JniEnv->ExceptionDescribe();
            JniEnv->ExceptionClear();
        }
    }
    
    

Log in to reply