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
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.
- 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)
- switch (orientation) {
- case ScreenOrientationLockPortrait:
- break;
- case ScreenOrientationLockLandscape:
- break;
- case ScreenOrientationAuto:
- default:
- 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);
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!
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
- };
- virtual ~QmlApplicationViewer();
- void setOrientation(ScreenOrientation orientation);
- void showExpanded();
- private:
- class QmlApplicationViewerPrivate *m_d;
- };
You must log in to post a reply. Not a member yet? Register here!



