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!



  • [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);
    @



  • 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
    };

    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;
    };@



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.