Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. What's wrong with alignment?
Qt 6.11 is out! See what's new in the release blog

What's wrong with alignment?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
12 Posts 4 Posters 3.3k Views 1 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.
  • R r3d9u11

    Hello, everyone!
    Could somebody explain what is wrong with code below, please?
    Why the "Right Button" is still displaying on the left side?

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import QtQuick.Layouts 1.12
    import QtQuick.Controls 2.12
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
        ColumnLayout {
            anchors.fill: parent
            RowLayout {
                Button { text: qsTr("Left Button");  Layout.alignment: Qt.AlignLeft  }
                Button { text: qsTr("Right Button"); Layout.alignment: Qt.AlignRight }
            }
        }
    }
    

    Thanks!

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by
    #2

    @r3d9u11
    because your RowLayout (inside the ColumnLayout) is missing a width or "Layout.fillWidth: true", thus it has the minimal size

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    R 1 Reply Last reply
    2
    • raven-worxR raven-worx

      @r3d9u11
      because your RowLayout (inside the ColumnLayout) is missing a width or "Layout.fillWidth: true", thus it has the minimal size

      R Offline
      R Offline
      r3d9u11
      wrote on last edited by r3d9u11
      #3

      @raven-worx
      I'm afraid no, for example

      ColumnLayout {
              anchors.fill: parent
              RowLayout {
                  Rectangle { Layout.fillWidth: true; height: 50; color: "red" }
              }
          }
      

      will draw rectangle to whole width of application's window

      And even with "Layout.fillWidth: true" nothing changed:

      ColumnLayout {
              anchors.fill: parent
              RowLayout {
                  Layout.fillWidth: true
                  Button { text: qsTr("Left Button");  Layout.alignment: Qt.AlignLeft  }
                  Button { text: qsTr("Right Button"); Layout.alignment: Qt.AlignRight }
              }
          }
      

      the "Right Button" is still on the left side

      Something wrong with Qt ? (I'm on 5.15.2)

      J.HilkJ 1 Reply Last reply
      0
      • R r3d9u11

        @raven-worx
        I'm afraid no, for example

        ColumnLayout {
                anchors.fill: parent
                RowLayout {
                    Rectangle { Layout.fillWidth: true; height: 50; color: "red" }
                }
            }
        

        will draw rectangle to whole width of application's window

        And even with "Layout.fillWidth: true" nothing changed:

        ColumnLayout {
                anchors.fill: parent
                RowLayout {
                    Layout.fillWidth: true
                    Button { text: qsTr("Left Button");  Layout.alignment: Qt.AlignLeft  }
                    Button { text: qsTr("Right Button"); Layout.alignment: Qt.AlignRight }
                }
            }
        

        the "Right Button" is still on the left side

        Something wrong with Qt ? (I'm on 5.15.2)

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #4

        @r3d9u11
        if you take a look in the documentation
        https://doc.qt.io/qt-5/qml-qtquick-layouts-rowlayout.html#details

        you will see, that Components inside the Layout can, or better should have one or more of those properties.

        Without them the Layout does not know how to arrange them

        You Buttons only have Layout.alignment set, no width, no height, no implicit width to height no fill width and height


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        R 1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @r3d9u11
          if you take a look in the documentation
          https://doc.qt.io/qt-5/qml-qtquick-layouts-rowlayout.html#details

          you will see, that Components inside the Layout can, or better should have one or more of those properties.

          Without them the Layout does not know how to arrange them

          You Buttons only have Layout.alignment set, no width, no height, no implicit width to height no fill width and height

          R Offline
          R Offline
          r3d9u11
          wrote on last edited by r3d9u11
          #5

          @J-Hilk
          Looks like problem not with Layout and(or) child Items size. But with nested Layouts.
          For example this qml works fine:

          ColumnLayout {
                  anchors.fill: parent
                  //RowLayout {
                      //Layout.fillWidth: true
                      Button { text: qsTr("Left Button");  Layout.alignment: Qt.AlignLeft  }
                      Button { text: qsTr("Right Button"); Layout.alignment: Qt.AlignRight }
                  //}
              }
          

          Ok, just for experiment I've defined some of these properties, and nothing changes with positioning.
          Right button is still on the left side:

          ColumnLayout {
                 anchors.fill: parent
                 RowLayout {
                     Layout.fillWidth: true
                     Button {
                         Layout.minimumWidth: 100
                         Layout.preferredWidth: 200
                         Layout.preferredHeight: 100
                         text: qsTr("Left Button");
                         Layout.alignment: Qt.AlignLeft
                     }
                     Button {
                         Layout.minimumWidth: 100
                         Layout.preferredWidth: 200
                         Layout.preferredHeight: 100
                         text: qsTr("Right Button");
                         Layout.alignment: Qt.AlignLeft
                     }
                 }
             }
          
          J.HilkJ 1 Reply Last reply
          1
          • R r3d9u11

            @J-Hilk
            Looks like problem not with Layout and(or) child Items size. But with nested Layouts.
            For example this qml works fine:

            ColumnLayout {
                    anchors.fill: parent
                    //RowLayout {
                        //Layout.fillWidth: true
                        Button { text: qsTr("Left Button");  Layout.alignment: Qt.AlignLeft  }
                        Button { text: qsTr("Right Button"); Layout.alignment: Qt.AlignRight }
                    //}
                }
            

            Ok, just for experiment I've defined some of these properties, and nothing changes with positioning.
            Right button is still on the left side:

            ColumnLayout {
                   anchors.fill: parent
                   RowLayout {
                       Layout.fillWidth: true
                       Button {
                           Layout.minimumWidth: 100
                           Layout.preferredWidth: 200
                           Layout.preferredHeight: 100
                           text: qsTr("Left Button");
                           Layout.alignment: Qt.AlignLeft
                       }
                       Button {
                           Layout.minimumWidth: 100
                           Layout.preferredWidth: 200
                           Layout.preferredHeight: 100
                           text: qsTr("Right Button");
                           Layout.alignment: Qt.AlignLeft
                       }
                   }
               }
            
            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #6

            @r3d9u11 Actually, correct me if I'm wrong, I think what you want is some kind of spacer item ?

            ColumnLayout {
                        anchors.fill: parent
                        RowLayout {
                            Layout.fillWidth: true
                            Button { text: qsTr("Left Button");  Layout.alignment: Qt.AlignLeft; }
                            Item {
                                // spacer item
                                Layout.fillWidth: true
                                Layout.fillHeight: true
                                Rectangle { anchors.fill: parent; color: "#ffaaaa" } // to visualize the spacer
                            }
                            Button { text: qsTr("Right Button"); Layout.alignment: Qt.AlignRight ;}
                        }
                    }
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            R 1 Reply Last reply
            2
            • J.HilkJ J.Hilk

              @r3d9u11 Actually, correct me if I'm wrong, I think what you want is some kind of spacer item ?

              ColumnLayout {
                          anchors.fill: parent
                          RowLayout {
                              Layout.fillWidth: true
                              Button { text: qsTr("Left Button");  Layout.alignment: Qt.AlignLeft; }
                              Item {
                                  // spacer item
                                  Layout.fillWidth: true
                                  Layout.fillHeight: true
                                  Rectangle { anchors.fill: parent; color: "#ffaaaa" } // to visualize the spacer
                              }
                              Button { text: qsTr("Right Button"); Layout.alignment: Qt.AlignRight ;}
                          }
                      }
              
              R Offline
              R Offline
              r3d9u11
              wrote on last edited by r3d9u11
              #7

              @J-Hilk
              Yeah, the workaround with one spacer works fine.
              Thank you!

              Spacer is doing the job even if alignment is not setted obviously:

              RowLayout {
                          Button { text: qsTr("Left Button") }
                          Item { Layout.fillWidth: true }
                          Button { text: qsTr("Right Button") }
                      }
              

              But I still don't understand the logic of alignment inside a nested layout (because, as I showed, example with non-nested layout works fine).

              Looks like a bug.

              raven-worxR 1 Reply Last reply
              2
              • R r3d9u11

                @J-Hilk
                Yeah, the workaround with one spacer works fine.
                Thank you!

                Spacer is doing the job even if alignment is not setted obviously:

                RowLayout {
                            Button { text: qsTr("Left Button") }
                            Item { Layout.fillWidth: true }
                            Button { text: qsTr("Right Button") }
                        }
                

                But I still don't understand the logic of alignment inside a nested layout (because, as I showed, example with non-nested layout works fine).

                Looks like a bug.

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #8

                @r3d9u11
                strange. Layout.fillWidth: true in the RowLayout should work, cant tell you why it doesn't.
                This works though:

                ColumnLayout {
                        anchors.fill: parent
                        RowLayout {
                            Layout.minimumWidth: parent.width
                            Button { text: qsTr("Left Button");  Layout.alignment: Qt.AlignLeft  }
                            Button { text: qsTr("Right Button"); Layout.alignment: Qt.AlignRight }
                        }
                }
                

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                R 2 Replies Last reply
                3
                • raven-worxR raven-worx

                  @r3d9u11
                  strange. Layout.fillWidth: true in the RowLayout should work, cant tell you why it doesn't.
                  This works though:

                  ColumnLayout {
                          anchors.fill: parent
                          RowLayout {
                              Layout.minimumWidth: parent.width
                              Button { text: qsTr("Left Button");  Layout.alignment: Qt.AlignLeft  }
                              Button { text: qsTr("Right Button"); Layout.alignment: Qt.AlignRight }
                          }
                  }
                  
                  R Offline
                  R Offline
                  r3d9u11
                  wrote on last edited by
                  #9

                  @raven-worx Thank you!
                  This solution works fine, too.
                  And looks a bit better.

                  Looks something wrong with "Layout.fillWidth" property.

                  1 Reply Last reply
                  0
                  • raven-worxR raven-worx

                    @r3d9u11
                    strange. Layout.fillWidth: true in the RowLayout should work, cant tell you why it doesn't.
                    This works though:

                    ColumnLayout {
                            anchors.fill: parent
                            RowLayout {
                                Layout.minimumWidth: parent.width
                                Button { text: qsTr("Left Button");  Layout.alignment: Qt.AlignLeft  }
                                Button { text: qsTr("Right Button"); Layout.alignment: Qt.AlignRight }
                            }
                    }
                    
                    R Offline
                    R Offline
                    r3d9u11
                    wrote on last edited by r3d9u11
                    #10

                    @raven-worx
                    Note for "Layout.minimumWidth: parent.width": I was found another strange behavior: if width of application's window was increased (for example from 1024 to 1280), and returned back (from 1280 to 1024), then "minimumWidth" sill have maximum size (1280).

                    So, all Controls on the right side will disappear (because their positions are out of application's width).

                    I don't know why, looks like another bug.

                    So, there is one acceptable solution: workaround with additional control Spacer.

                    raven-worxR 1 Reply Last reply
                    0
                    • R r3d9u11

                      @raven-worx
                      Note for "Layout.minimumWidth: parent.width": I was found another strange behavior: if width of application's window was increased (for example from 1024 to 1280), and returned back (from 1280 to 1024), then "minimumWidth" sill have maximum size (1280).

                      So, all Controls on the right side will disappear (because their positions are out of application's width).

                      I don't know why, looks like another bug.

                      So, there is one acceptable solution: workaround with additional control Spacer.

                      raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by raven-worx
                      #11

                      @r3d9u11
                      and when you bind the row layout's width to the parent width?

                      RowLayout {
                         width: parent.width
                         ...
                      }
                      

                      So, all Controls on the right side will disappear (because their positions are out of application's width).
                      I don't know why, looks like another bug.

                      Not a bug, the minimumWidth will rise, but never shrink.

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        chengfeng-xie
                        wrote on last edited by
                        #12

                        For the Layout.fillWidth: true approach to work, we need to add:

                        Layout.maximumWidth: Number.POSITIVE_INFINITY
                        

                        to the nested RowLayout (credit to this post).

                        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