December 28, 2010

Onddo Onddo
Lab Rat
12 posts

Lock orientation of the phone for specific QML pages

 

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!

5 replies

December 28, 2010

Milot Shala Milot Shala
Lab Rat
371 posts

Onddo wrote:
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!

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.

  1. void QmlApplicationViewer::setOrientation(ScreenOrientation orientation)
  2. {
  3. #ifdef Q_OS_SYMBIAN
  4.     if (orientation != ScreenOrientationAuto) {
  5. #if defined(ORIENTATIONLOCK)
  6.         const CAknAppUiBase::TAppUiOrientation uiOrientation =
  7.                 (orientation == ScreenOrientationLockPortrait) ? CAknAppUi::EAppUiOrientationPortrait
  8.                     : CAknAppUi::EAppUiOrientationLandscape;
  9.         CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
  10.         TRAPD(error,
  11.             if (appUi)
  12.                 appUi->SetOrientationL(uiOrientation);
  13.         );
  14.         Q_UNUSED(error)
  15. #else // ORIENTATIONLOCK
  16.         qWarning("'ORIENTATIONLOCK' needs to be defined on Symbian when locking the orientation.");
  17. #endif // ORIENTATIONLOCK
  18.     }
  19. #elif defined(Q_WS_MAEMO_5)
  20.     Qt::WidgetAttribute attribute;
  21.     switch (orientation) {
  22.     case ScreenOrientationLockPortrait:
  23.         attribute = Qt::WA_Maemo5PortraitOrientation;
  24.         break;
  25.     case ScreenOrientationLockLandscape:
  26.         attribute = Qt::WA_Maemo5LandscapeOrientation;
  27.         break;
  28.     case ScreenOrientationAuto:
  29.     default:
  30.         attribute = Qt::WA_Maemo5AutoOrientation;
  31.         break;
  32.     }
  33.     setAttribute(attribute, true);
  34. #else // Q_OS_SYMBIAN
  35.     Q_UNUSED(orientation);
  36. #endif // Q_OS_SYMBIAN
  37. }

After that it is very easy to use it:

  1. QmlApplicationViewer viewer;
  2. viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);

January 28, 2011

Onddo Onddo
Lab Rat
12 posts

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!

January 28, 2011

ngocketit ngocketit
Lab Rat
22 posts

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

  1. #include <QtDeclarative/QDeclarativeView>
  2.  
  3. class QmlApplicationViewer : public QDeclarativeView
  4. {
  5.     Q_OBJECT
  6.  
  7. public:
  8.     enum ScreenOrientation {
  9.         ScreenOrientationLockPortrait,
  10.         ScreenOrientationLockLandscape,
  11.         ScreenOrientationAuto
  12.     };
  13.  
  14.     explicit QmlApplicationViewer(QWidget *parent = 0);
  15.     virtual ~QmlApplicationViewer();
  16.  
  17.     void setMainQmlFile(const QString &file);
  18.     void addImportPath(const QString &path);
  19.     void setOrientation(ScreenOrientation orientation);
  20.     void showExpanded();
  21.  
  22. private:
  23.     class QmlApplicationViewerPrivate *m_d;
  24. };

April 19, 2011

huluyige huluyige
Lab Rat
45 posts

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.

April 25, 2011

ZapB ZapB
Ant Farmer
1442 posts

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.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

 
  ‹‹ QtMultimediaKit Video element      the font in QML is not smooth, in windows ››

You must log in to post a reply. Not a member yet? Register here!