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. Adjusting for screen orientation with QML Window
Forum Updated to NodeBB v4.3 + New Features

Adjusting for screen orientation with QML Window

Scheduled Pinned Locked Moved Solved Mobile and Embedded
iosqml
8 Posts 3 Posters 6.9k 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.
  • D Offline
    D Offline
    DRoscoe
    wrote on last edited by
    #1

    I have a custom QML Window that sets its initial height and width based on the screen orientation when the window is created:

      property bool is_landscape: mainWindow.width > mainWindow.height
      width: is_landscape ? mainWindow.width/3 : mainWindow.width/2
      height: mainWindow.height/4
    

    This works fine. The problem comes when the screen orientation is changed after the window is displayed. With a QML Rectangle object, I can use states to capture when the orientation changes:

        states: [
          State {
            name: "Landscape"
            when: splash_screen.width > splash_screen.height
    
            PropertyChanges {
              target: splash_screen
              image_file: "./SplashScreen-landscape.png"
            }
          },
          State {
           name: "Portrait"
           when: splash_screen.height > splash_screen.width
    
           PropertyChanges {
              target: splash_screen
              image_file: "./SplashScreen-portrait.png"
            }
          }
        ]
    

    However, I cannot use states with a QML Window object, and the window position and dimensions become messed up when the screen orientation changes.

    How can I resize my window when screen orientation changes?

    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      Have you tried Screen.orientation? Note the part about orientationUpdateMask.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      D 1 Reply Last reply
      0
      • jeremy_kJ jeremy_k

        Have you tried Screen.orientation? Note the part about orientationUpdateMask.

        D Offline
        D Offline
        DRoscoe
        wrote on last edited by
        #3

        @jeremy_k I've come across it, but I am not clear how to use it. The documentation is less than clear. What I want is to re-center my dialog window based on the screen orientation when it changes, and to optionally change height and width properties of the window

        kshegunovK 1 Reply Last reply
        0
        • D DRoscoe

          @jeremy_k I've come across it, but I am not clear how to use it. The documentation is less than clear. What I want is to re-center my dialog window based on the screen orientation when it changes, and to optionally change height and width properties of the window

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @DRoscoe said in Adjusting for screen orientation with QML Window:

          What I want is to re-center my dialog window based on the screen orientation when it changes

          Why don't you just anchor the dialog to the center point, or am I missing something here?

          Read and abide by the Qt Code of Conduct

          jeremy_kJ 1 Reply Last reply
          0
          • kshegunovK kshegunov

            @DRoscoe said in Adjusting for screen orientation with QML Window:

            What I want is to re-center my dialog window based on the screen orientation when it changes

            Why don't you just anchor the dialog to the center point, or am I missing something here?

            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #5

            @kshegunov said in Adjusting for screen orientation with QML Window:

            @DRoscoe said in Adjusting for screen orientation with QML Window:

            What I want is to re-center my dialog window based on the screen orientation when it changes

            Why don't you just anchor the dialog to the center point, or am I missing something here?

            Item anchors aren't available for Window{}.

            I wonder if this will work:

            Window {
                id: window
                visible: true
                Screen.orientationUpdateMask:  Qt.LandscapeOrientation | Qt.PortraitOrientation
                x: (Screen.width - window.width) / 2
                y: (Screen.height - window.height) / 2
            
                Text { anchors.centerIn: parent; text: "X" }
            }

            Asking a question about code? http://eel.is/iso-c++/testcase/

            kshegunovK D 2 Replies Last reply
            2
            • jeremy_kJ jeremy_k

              @kshegunov said in Adjusting for screen orientation with QML Window:

              @DRoscoe said in Adjusting for screen orientation with QML Window:

              What I want is to re-center my dialog window based on the screen orientation when it changes

              Why don't you just anchor the dialog to the center point, or am I missing something here?

              Item anchors aren't available for Window{}.

              I wonder if this will work:

              Window {
                  id: window
                  visible: true
                  Screen.orientationUpdateMask:  Qt.LandscapeOrientation | Qt.PortraitOrientation
                  x: (Screen.width - window.width) / 2
                  y: (Screen.height - window.height) / 2
              
                  Text { anchors.centerIn: parent; text: "X" }
              }
              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @jeremy_k said in Adjusting for screen orientation with QML Window:

              Item anchors aren't available for Window{}.

              Yeah, so I discover.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • jeremy_kJ jeremy_k

                @kshegunov said in Adjusting for screen orientation with QML Window:

                @DRoscoe said in Adjusting for screen orientation with QML Window:

                What I want is to re-center my dialog window based on the screen orientation when it changes

                Why don't you just anchor the dialog to the center point, or am I missing something here?

                Item anchors aren't available for Window{}.

                I wonder if this will work:

                Window {
                    id: window
                    visible: true
                    Screen.orientationUpdateMask:  Qt.LandscapeOrientation | Qt.PortraitOrientation
                    x: (Screen.width - window.width) / 2
                    y: (Screen.height - window.height) / 2
                
                    Text { anchors.centerIn: parent; text: "X" }
                }
                D Offline
                D Offline
                DRoscoe
                wrote on last edited by
                #7

                @jeremy_k That did the trick! Thanks!

                1 Reply Last reply
                0
                • jeremy_kJ Offline
                  jeremy_kJ Offline
                  jeremy_k
                  wrote on last edited by
                  #8

                  Happy to hear it. I'm lacking a device with a rotatable screen at the moment, so that recipe involved a little faith.

                  Asking a question about code? http://eel.is/iso-c++/testcase/

                  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