Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Lock orientation of the phone for specific QML pages
Forum Updated to NodeBB v4.3 + New Features

Lock orientation of the phone for specific QML pages

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 5 Posters 8.6k 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.
  • O Offline
    O Offline
    Onddo
    wrote on last edited by
    #1

    Hi - I'm developing a QML app for Symbian. I'm loading my QML pages dynamically, as follows:

    function loadPage(qmlFileString) {
    //console.log("Load Page:"+ qmlFileString)
    var loadedComponent = Qt.createComponent(qmlFileString);
    if (loadedComponent.status == Component.Ready) {
    var loadedPage = loadedComponent.createObject(mainRectangle);
    }
    }

    Depending of which QML page is loaded, I would like to lock the orientation of the phone so certain features work better. I was wondering if there's a possibility to lock the orientation of the phone screen for specific QML pages?

    Thanks!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      milot.shala
      wrote on last edited by
      #2

      [quote author="Onddo" date="1293537905"]Hi - I'm developing a QML app for Symbian. I'm loading my QML pages dynamically, as follows:

      function loadPage(qmlFileString) {
      //console.log("Load Page:"+ qmlFileString)
      var loadedComponent = Qt.createComponent(qmlFileString);
      if (loadedComponent.status == Component.Ready) {
      var loadedPage = loadedComponent.createObject(mainRectangle);
      }
      }

      Depending of which QML page is loaded, I would like to lock the orientation of the phone so certain features work better. I was wondering if there's a possibility to lock the orientation of the phone screen for specific QML pages?

      Thanks! [/quote]

      I think you should an Qt Quick Application first, new version of Qt Creator will create the code for locking your orientation automatically, but in case you are using the old version I am pasting the generated code below.

      @
      void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
      {
      #ifdef Q_OS_SYMBIAN
      if (orientation != ScreenOrientationAuto) {
      #if defined(ORIENTATIONLOCK)
      const CAknAppUiBase::TAppUiOrientation uiOrientation =
      (orientation == ScreenOrientationLockPortrait) ? CAknAppUi::EAppUiOrientationPortrait
      : CAknAppUi::EAppUiOrientationLandscape;
      CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
      TRAPD(error,
      if (appUi)
      appUi->SetOrientationL(uiOrientation);
      );
      Q_UNUSED(error)
      #else // ORIENTATIONLOCK
      qWarning("'ORIENTATIONLOCK' needs to be defined on Symbian when locking the orientation.");
      #endif // ORIENTATIONLOCK
      }
      #elif defined(Q_WS_MAEMO_5)
      Qt::WidgetAttribute attribute;
      switch (orientation) {
      case ScreenOrientationLockPortrait:
      attribute = Qt::WA_Maemo5PortraitOrientation;
      break;
      case ScreenOrientationLockLandscape:
      attribute = Qt::WA_Maemo5LandscapeOrientation;
      break;
      case ScreenOrientationAuto:
      default:
      attribute = Qt::WA_Maemo5AutoOrientation;
      break;
      }
      setAttribute(attribute, true);
      #else // Q_OS_SYMBIAN
      Q_UNUSED(orientation);
      #endif // Q_OS_SYMBIAN
      }
      @

      After that it is very easy to use it:

      @
      QmlApplicationViewer viewer;
      viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
      @

      1 Reply Last reply
      0
      • O Offline
        O Offline
        Onddo
        wrote on last edited by
        #3

        Thanks for the response. The problem is that I'm using QDeclarativeView rather than QmlApplicationViewer, so I can make Qt/QML connections via view.rootContext()->setContextProperty.

        I couldn't find any obvious API on QDeclarativeView to set/lock the orientation of the screen ... Does anyone know how to get around this?

        Thanks!

        1 Reply Last reply
        0
        • N Offline
          N Offline
          ngocketit
          wrote on last edited by
          #4

          You can use the same approach as QmlApplicationViewer does. QmlApplicationViewer generated by QtCreator, in fact, extends QDeclarativeView:

          @#include <QtDeclarative/QDeclarativeView>

          class QmlApplicationViewer : public QDeclarativeView
          {
          Q_OBJECT

          public:
          enum ScreenOrientation {
          ScreenOrientationLockPortrait,
          ScreenOrientationLockLandscape,
          ScreenOrientationAuto
          };

          explicit QmlApplicationViewer(QWidget *parent = 0);
          virtual ~QmlApplicationViewer();
          
          void setMainQmlFile&#40;const QString &file&#41;;
          void addImportPath(const QString &path);
          void setOrientation(ScreenOrientation orientation);
          void showExpanded();
          

          private:
          class QmlApplicationViewerPrivate *m_d;
          };@

          1 Reply Last reply
          0
          • H Offline
            H Offline
            huluyige
            wrote on last edited by
            #5

            Hi, I understand how to use QmlApplicationViewer to set screen orientation, but I don't understang how to use it to lock screen orientation just for specific QML pages, not for the whole application. (specially for a load in the main.qml, to loader other pages on the fly)

            Thanks.

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              ZapB
              wrote on last edited by
              #6

              You could call the setOrientation() function from your QML scene as needed. You would need to make it a slot or mark it as a Q_INVOKABLE I think.

              Edit: And expose the object via QDeclarativeContext::setContextProperty.

              Nokia Certified Qt Specialist
              Interested in hearing about Qt related work

              1 Reply Last reply
              0

              • Login

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