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. positioning items within a RowLayout

positioning items within a RowLayout

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 2.4k Views
  • 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 30 May 2023, 01:33 last edited by
    #1

    Hi all -

    I'm trying to code a RowLayout (that will be within another layout). The RowLayout will contain 3 items: two Text, and one Image. I'd like the 2nd Text and the Image to be pushed to the right.

    I can accomplish this with the use of a "spacer" item, but I'm wondering whether there's a preferred alternative.

    With the spacer Item, I get what I want:
    status.JPG
    Without the spacer Item, I get this:
    statusbad.JPG
    Here's my code:

    import QtQuick
    import QtQuick.Controls
    import QtQuick.Layouts
    
    ApplicationWindow {
        id: mainWindow
        visible: true
        width: 800
        height: 640
    
        ColumnLayout {
            id: mainLayout
            anchors.fill: parent
            RowLayout {
                spacing: 8
                Layout.preferredWidth: mainLayout.width
                Layout.alignment: Qt.AlignTop
                Layout.margins: 32
    
                Text {
                    Layout.alignment: Qt.AlignLeft
                    text: "SITE STATUS"
                }
    // I'd rather not have to use this Item if possible.
    //            Item {
    //                Layout.fillWidth: true
    //                height: 1
    //            }
    
                Text {
                    Layout.alignment: Qt.AlignRight
                    text: "My City, My State | 56 F"
                }
    
                Image {
                    source: "qrc:/Sunny.svg"
                    Layout.alignment: Qt.AlignRight
                }
            }
        }
    }
    

    Is there in fact, a better way to do this?

    Thanks...

    L A 2 Replies Last reply 30 May 2023, 10:25
    0
    • M mzimmers
      30 May 2023, 17:29

      @lemons thanks for the suggestion. So, I guess when using layouts, it's not enough to say "align right;" you need something to fill the space?

      L Offline
      L Offline
      lemons
      wrote on 31 May 2023, 06:19 last edited by
      #4

      @mzimmers if you don't add Layout.fillWidth: true to one of the layouts childs, the space gets distributed evenly.
      In this case, each item defaults to left alignment. If you want to change this, you can set Layout.alignment: Qt.AlignHCenter or Layout.alignment: Qt.AlignRight to the childs that shouldn't be aligned to the left, inside their assigned space.

      If you want to have empty space, you have to decide on your use case.
      Usually you don't need a spare item just for spacing, as you can make another item grow to archive the same result.

      M 1 Reply Last reply 31 May 2023, 13:52
      1
      • M mzimmers
        30 May 2023, 01:33

        Hi all -

        I'm trying to code a RowLayout (that will be within another layout). The RowLayout will contain 3 items: two Text, and one Image. I'd like the 2nd Text and the Image to be pushed to the right.

        I can accomplish this with the use of a "spacer" item, but I'm wondering whether there's a preferred alternative.

        With the spacer Item, I get what I want:
        status.JPG
        Without the spacer Item, I get this:
        statusbad.JPG
        Here's my code:

        import QtQuick
        import QtQuick.Controls
        import QtQuick.Layouts
        
        ApplicationWindow {
            id: mainWindow
            visible: true
            width: 800
            height: 640
        
            ColumnLayout {
                id: mainLayout
                anchors.fill: parent
                RowLayout {
                    spacing: 8
                    Layout.preferredWidth: mainLayout.width
                    Layout.alignment: Qt.AlignTop
                    Layout.margins: 32
        
                    Text {
                        Layout.alignment: Qt.AlignLeft
                        text: "SITE STATUS"
                    }
        // I'd rather not have to use this Item if possible.
        //            Item {
        //                Layout.fillWidth: true
        //                height: 1
        //            }
        
                    Text {
                        Layout.alignment: Qt.AlignRight
                        text: "My City, My State | 56 F"
                    }
        
                    Image {
                        source: "qrc:/Sunny.svg"
                        Layout.alignment: Qt.AlignRight
                    }
                }
            }
        }
        

        Is there in fact, a better way to do this?

        Thanks...

        L Offline
        L Offline
        lemons
        wrote on 30 May 2023, 10:25 last edited by
        #2

        @mzimmers said in positioning items within a RowLayout:

        Image {
        source: "qrc:/Sunny.svg"
        Layout.alignment: Qt.AlignRight
        }

        Just add Layout.fillWidth to the first entry instead of adding an additional spacer.
        Note: you also don't need the alignments, if those entries aren't wider than their content.

        RowLayout {
           spacing: 8
           Layout.preferredWidth: mainLayout.width
           Layout.alignment: Qt.AlignTop
           Layout.margins: 32
        
           Text {
               Layout.fillWidth: true
               text: "SITE STATUS"
           }
           Text {
               text: "My City, My State | 56 F"
           }
           Image {
               source: "qrc:/Sunny.svg"
           }
        }
        
        M 1 Reply Last reply 30 May 2023, 17:29
        1
        • L lemons
          30 May 2023, 10:25

          @mzimmers said in positioning items within a RowLayout:

          Image {
          source: "qrc:/Sunny.svg"
          Layout.alignment: Qt.AlignRight
          }

          Just add Layout.fillWidth to the first entry instead of adding an additional spacer.
          Note: you also don't need the alignments, if those entries aren't wider than their content.

          RowLayout {
             spacing: 8
             Layout.preferredWidth: mainLayout.width
             Layout.alignment: Qt.AlignTop
             Layout.margins: 32
          
             Text {
                 Layout.fillWidth: true
                 text: "SITE STATUS"
             }
             Text {
                 text: "My City, My State | 56 F"
             }
             Image {
                 source: "qrc:/Sunny.svg"
             }
          }
          
          M Offline
          M Offline
          mzimmers
          wrote on 30 May 2023, 17:29 last edited by
          #3

          @lemons thanks for the suggestion. So, I guess when using layouts, it's not enough to say "align right;" you need something to fill the space?

          L 1 Reply Last reply 31 May 2023, 06:19
          0
          • M mzimmers
            30 May 2023, 17:29

            @lemons thanks for the suggestion. So, I guess when using layouts, it's not enough to say "align right;" you need something to fill the space?

            L Offline
            L Offline
            lemons
            wrote on 31 May 2023, 06:19 last edited by
            #4

            @mzimmers if you don't add Layout.fillWidth: true to one of the layouts childs, the space gets distributed evenly.
            In this case, each item defaults to left alignment. If you want to change this, you can set Layout.alignment: Qt.AlignHCenter or Layout.alignment: Qt.AlignRight to the childs that shouldn't be aligned to the left, inside their assigned space.

            If you want to have empty space, you have to decide on your use case.
            Usually you don't need a spare item just for spacing, as you can make another item grow to archive the same result.

            M 1 Reply Last reply 31 May 2023, 13:52
            1
            • M mzimmers
              30 May 2023, 01:33

              Hi all -

              I'm trying to code a RowLayout (that will be within another layout). The RowLayout will contain 3 items: two Text, and one Image. I'd like the 2nd Text and the Image to be pushed to the right.

              I can accomplish this with the use of a "spacer" item, but I'm wondering whether there's a preferred alternative.

              With the spacer Item, I get what I want:
              status.JPG
              Without the spacer Item, I get this:
              statusbad.JPG
              Here's my code:

              import QtQuick
              import QtQuick.Controls
              import QtQuick.Layouts
              
              ApplicationWindow {
                  id: mainWindow
                  visible: true
                  width: 800
                  height: 640
              
                  ColumnLayout {
                      id: mainLayout
                      anchors.fill: parent
                      RowLayout {
                          spacing: 8
                          Layout.preferredWidth: mainLayout.width
                          Layout.alignment: Qt.AlignTop
                          Layout.margins: 32
              
                          Text {
                              Layout.alignment: Qt.AlignLeft
                              text: "SITE STATUS"
                          }
              // I'd rather not have to use this Item if possible.
              //            Item {
              //                Layout.fillWidth: true
              //                height: 1
              //            }
              
                          Text {
                              Layout.alignment: Qt.AlignRight
                              text: "My City, My State | 56 F"
                          }
              
                          Image {
                              source: "qrc:/Sunny.svg"
                              Layout.alignment: Qt.AlignRight
                          }
                      }
                  }
              }
              

              Is there in fact, a better way to do this?

              Thanks...

              A Offline
              A Offline
              Atr0p0s
              wrote on 31 May 2023, 12:03 last edited by Atr0p0s
              #5

              @mzimmers You can also try to use Positioning with Anchors without layouts, where you can bind items together.

              For example, this code

              import QtQuick
              
              Window {
                  id: mainWindow
                  visible: true
                  width: 800
                  height: 640
              
                  Rectangle {
                      id: header
                      width: parent.width
                      height: 80
                      anchors {
                          left: parent.left
                          right: parent.right
                          top: parent.top
                      }
              
                      Text {
                          text: "SITE STATUS"
                          anchors {
                              verticalCenter: img.verticalCenter
                              left: parent.left
                              leftMargin: 10
                          }
                      }
              
                      Text {
                          text: "My City, My State | 56 F"
                          anchors {
                              verticalCenter: img.verticalCenter
                              right: img.left
                              rightMargin: 10
                          }
                      }
              
                      Image {
                          id: img
                          source: "qrc:/sun.jpg"
                          anchors {
                              right: parent.right
                              top: parent.top
                              topMargin: 10
                              rightMargin: 10
                          }
                      }
                  }
              }
              

              gives me such result:
              forum.png

              1 Reply Last reply
              2
              • L lemons
                31 May 2023, 06:19

                @mzimmers if you don't add Layout.fillWidth: true to one of the layouts childs, the space gets distributed evenly.
                In this case, each item defaults to left alignment. If you want to change this, you can set Layout.alignment: Qt.AlignHCenter or Layout.alignment: Qt.AlignRight to the childs that shouldn't be aligned to the left, inside their assigned space.

                If you want to have empty space, you have to decide on your use case.
                Usually you don't need a spare item just for spacing, as you can make another item grow to archive the same result.

                M Offline
                M Offline
                mzimmers
                wrote on 31 May 2023, 13:52 last edited by
                #6

                @lemons said in positioning items within a RowLayout:

                inside their assigned space.

                Ahhhh...I didn't realize this. I thought the alignment was relevant to the entire layout, not merely the space used by the element. Thanks for the epiphany.

                @Atr0p0s thanks for the suggestion. My design is relatively committed to Layouts (for the most part), but I do like your implementation as well.

                1 Reply Last reply
                1
                • M mzimmers has marked this topic as solved on 31 May 2023, 13:52

                4/6

                31 May 2023, 06:19

                • Login

                • Login or register to search.
                4 out of 6
                • First post
                  4/6
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved