Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Using C++ to rotate Screen / QSensor::setUserOrientation(90);
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved Mobile and Embedded
2 Posts 1 Posters 1.7k Views 2 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.
  • T Offline
    T Offline
    Thomas Cameon
    wrote on last edited by
    #1

    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 :)

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Thomas Cameon
      wrote on last edited by
      #2

      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
                      }
                  }
              ]
          }
      }
      
      
      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