Programatically setting screen orientation on Android
-
Hi,
I'm porting an app from QT5.15 to QT6.3. A central function of my application is the setting of the screen orientation. According to the settings in a configuration file, the application must lock to landscape or portrait mode (or their inverse counterparts. For details look at TPanel). With QT5 I did:
QScreen *screen = QGuiApplication::primaryScreen(); QOrientationReading::Orientation ori = QOrientationReading::Undefined; Qt::ScreenOrientations mask = Qt::PrimaryOrientation; QOrientationSensor qOri; QOrientationReading *pORead = qOri.reading(); if (pORead) { ori = pORead->orientation(); switch(ori) { case QOrientationReading::LeftUp: mask = Qt::InvertedLandscapeOrientation; break; case QOrientationReading::RightUp: mask = Qt::LandscapeOrientation; break; case QOrientationReading::TopDown: mask = Qt::InvertedPortraitOrientation; break; default: mask = Qt::PortraitOrientation; } } if (pmanager->getSettings()->isPortrait()) // portrait? { if (mask == Qt::InvertedPortraitOrientation) screen->setOrientationUpdateMask(Qt::InvertedPortraitOrientation); else screen->setOrientationUpdateMask(Qt::PortraitOrientation); } else { if (mask == Qt::InvertedLandscapeOrientation) screen->setOrientationUpdateMask(Qt::InvertedLandscapeOrientation); else screen->setOrientationUpdateMask(Qt::LandscapeOrientation); }
But in QT6 the method setOrientationUpdateMask() does not exist any more and I was not able to find an alternative way to implement the same behavior.
Is there something similar in QT6.3 and if so how should it be implemented?A.T.
-
Usually I don't comment my own postings, but because there seems to be no answer I thing I should post the solution I found. To make it short: There is no working solution with Qt 6.3.1. Instead this must be done with a small Java program. To see the full code look at Orientation.java.
This can be called with C++ like this.void TPageManager::initOrientation() { int rotate = getSettings()->getRotate(); // Is base equal to LANDSCAPE or PORTRAIT? #ifdef QT5_ONLY QAndroidJniObject activity = QtAndroid::androidActivity(); QAndroidJniObject::callStaticMethod<void>("org/qtproject/theosys/Orientation", "Init", "(Landroid/app/Activity;I)V", activity.object(), rotate); #else QJniObject activity = QNativeInterface::QAndroidApplication::context(); QJniObject::callStaticMethod<void>("org/qtproject/theosys/Orientation", "Init", "(Landroid/app/Activity;I)V", activity.object(), rotate); #endif activity.callStaticMethod<void>("org/qtproject/theosys/Orientation", "InstallOrientationListener", "()V"); }
To receive a signal with each change of the orientation, define the following function.
JNIEXPORT void JNICALL Java_org_qtproject_theosys_Orientation_informTPanelOrientation(JNIEnv */*env*/, jclass /*clazz*/, jint orientation) { /* Do something with the "orientation" */ }
A.T.
-
@TheoSys BTW: this is how I'm setting a fix Orientation on Android to Landscape in Qt 5.15:
QAndroidJniObject activity = QtAndroid::androidActivity(); if(activity.isValid()) { activity.callMethod<void>("setRequestedOrientation", "(I)V", 0); }
see also: https://falsinsoft.blogspot.com/2018/02/qml-change-android-screen-orientation.html
I know there must probably some adjustments for Qt 6... will do this later this year when porting my apps from 5 to 6