Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Programatically setting screen orientation on Android
Forum Updated to NodeBB v4.3 + New Features

Programatically setting screen orientation on Android

Scheduled Pinned Locked Moved Solved Mobile and Embedded
3 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheoSys
    wrote on 30 Jul 2022, 17:35 last edited by
    #1

    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.

    T 1 Reply Last reply 7 Aug 2022, 15:07
    0
    • T TheoSys
      30 Jul 2022, 17:35

      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.

      T Offline
      T Offline
      TheoSys
      wrote on 7 Aug 2022, 15:07 last edited by
      #2

      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.

      E 1 Reply Last reply 8 Aug 2022, 16:55
      2
      • T TheoSys
        7 Aug 2022, 15:07

        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.

        E Offline
        E Offline
        ekkescorner
        Qt Champions 2016
        wrote on 8 Aug 2022, 16:55 last edited by
        #3

        @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

        ekke ... Qt Champion 2016 | 2024 ... mobile business apps
        5.15 --> 6.8 https://t1p.de/ekkeChecklist
        QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved