Using C++ to rotate Screen / QSensor::setUserOrientation(90);



  • Re: Adjusting for screen orientation with QML Window

    Hi !
    I'm working on an imx6 device on Linux and I'm looking for a solution to rotate my screen. My supplier will install a sensor on the board to tell me if I'm in landscape or portrait, so I have to develop something that will rotate my UI. I saw that QSensor offers a way to force UserOrientation and I think it's good for us but I can't make it work. Here is my code :

    main.cpp

    # include <QSensor>
    ...
    //Portrait force
    QSensor* sensor = new QSensor(); // don't know what arguments to put here, doc says QByteArray &type, QObject *parent = Q_NULLPTR
    sensor->setAxesOrientationMode(QSensor::UserOrientation);
    sensor->setUserOrientation(90);
    

    What do you think of that solution, is there a better way for me to rotate my UI ?
    I'm using ApplicationWindow as a root item, and I developped my UI on android using a lot of anchors - and it works perfectly. So I don't want to rotate my UI because bottom and rigth anchors are out of screen, the only solution I found is to set width and height for all my Items - wich is awful :)



  • Sadly I came to a QML temporary solution, I will see later how to use Qt Sensors. Here is the solution I found : I sauw that rotating Page could be used instead of ApplicationWindow :

    main.qml

    import QtQuick 2.4
    import QtQuick.Layouts 1.1
    import QtQuick.Controls 2.0
    
    MyApplicationWindow {
        visible: true
        width: 400
        height: 300
    
        rotation: 90
    
        header: ToolBar {
            RowLayout {
                ToolButton {
                    text: "tool button"
                }
            }
        }
        Text {
            anchors.centerIn: parent
            text: "some text here"
        }
    }
    

    MyApplicationWindow.qml

    import QtQuick 2.4
    import QtQuick.Window 2.0
    import QtQuick.Controls 2.0
    
    Window {
        id: window
    
        property alias header: page.header
        property alias footer: page.footer
        property alias rotation: page.rotation
        default property alias _content: page.contentItem
    
        Page {
            id: page
    
            width: window.width
            height: window.height
            anchors.centerIn: parent
    
            states: [
                State {
                    name: "landscape"
                    when: page.rotation == 0
                },
                State {
                    name: "portrait"
                    when: page.rotation == 90
                    PropertyChanges {
                        target: page
                        width: window.height
                        height: window.width
                    }
                },
                State {
                    name: "invertedlandscape"
                    when: page.rotation == 180
                },
                State {
                    id: "invertedportrait"
                    when: page.rotation == 270
                    PropertyChanges {
                        target: page
                        width: window.height
                        height: window.width
                    }
                }
            ]
        }
    }
    
    

Log in to reply
 

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