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. Place items in GridLayout leaving empty column
QtWS25 Last Chance

Place items in GridLayout leaving empty column

Scheduled Pinned Locked Moved Solved QML and Qt Quick
gridlayoutemptyorganization
6 Posts 2 Posters 2.2k 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.
  • K Offline
    K Offline
    Kyeiv
    wrote on 5 Jul 2021, 11:49 last edited by
    #1

    I want to organize my elements in the following manner:

    |______text___button|

    So the text is centered and button aligned to right.

    My code:

    GridLayout 
            {
                Layout.fillWidth: true
                columns: 3
    
                Item {                
                	Layout.row: 0
                    Layout.column: 0
                }
                Text
                {
                    Layout.alignment: Qt.AlignHCenter
                    horizontalAlignment: Text.AlignHCenter
                    text: "text"
                    color: "white"
                    Layout.row: 0
                    Layout.column: 1
                }
                Button
    			{
                   
                    Layout.alignment: Qt.AlignRight
    				
                   
                    implicitHeight: 35
    	        implicitWidth: 35
                    Layout.row: 0
                    Layout.column: 2
    			}
            }
    

    But it organizes them like:
    |text___button___________|

    What can i do about it?

    O 1 Reply Last reply 6 Jul 2021, 01:56
    0
    • K Kyeiv
      7 Jul 2021, 08:44

      @ODБOï said in Place items in GridLayout leaving empty column:

      anchors.centerIn:parent

      unfortunately your solutions still shift the text to left or right.

      The only solution that i came up to and works but is ugly:

      GridLayout 
              {
                  Layout.fillWidth: true
                  columns: 3
      
                  Item {
                      Layout.fillWidth: true                
                  }
                  Text
                  {
                      Layout.fillWidth: true
                      Layout.alignment: Qt.AlignHCenter
                      anchors.horizontalCenter: parent.horizontalCenter
                      horizontalAlignment: Text.AlignHCenter
                      text: "text"
                      color: "white"
                  }
                  Button
      	    {
                      Layout.alignment: Qt.AlignRight
                      implicitHeight: 35
                      Layout.maximumWidth: 35
      	    }
              }
      
      O Offline
      O Offline
      ODБOï
      wrote on 7 Jul 2021, 08:52 last edited by
      #6

      @Kyeiv said in Place items in GridLayout leaving empty column:

      anchors.horizontalCenter: parent.horizontalCenter

      you can't use "anchors" inside an item that is direct child of a QML Layout, you should see a warning for that.

      This will put the text exactly in the center

          GridLayout{
                  anchors.fill : parent
                  columns : 3   
                  Item{ 
                     Layout.fillWidth:true
                     Layout.columnSpan  : 3
                      Text{
                         text : "Hello"   
                         anchors.centerIn:parent
                      }
                      Button{
                        anchors.right:parent.right
                        anchors.verticalCenter:parent.verticalCenter
                      }
                  }
          }
      
      1 Reply Last reply
      0
      • K Kyeiv
        5 Jul 2021, 11:49

        I want to organize my elements in the following manner:

        |______text___button|

        So the text is centered and button aligned to right.

        My code:

        GridLayout 
                {
                    Layout.fillWidth: true
                    columns: 3
        
                    Item {                
                    	Layout.row: 0
                        Layout.column: 0
                    }
                    Text
                    {
                        Layout.alignment: Qt.AlignHCenter
                        horizontalAlignment: Text.AlignHCenter
                        text: "text"
                        color: "white"
                        Layout.row: 0
                        Layout.column: 1
                    }
                    Button
        			{
                       
                        Layout.alignment: Qt.AlignRight
        				
                       
                        implicitHeight: 35
        	        implicitWidth: 35
                        Layout.row: 0
                        Layout.column: 2
        			}
                }
        

        But it organizes them like:
        |text___button___________|

        What can i do about it?

        O Offline
        O Offline
        ODБOï
        wrote on 6 Jul 2021, 01:56 last edited by
        #2

        @Kyeiv hi you can simply set attached property "Layout.fillWidth" to true in on your dummy Item
        see also "preferredWidth"
        https://doc.qt.io/qt-5/qml-qtquick-layouts-layout.html#attached-properties

        K 1 Reply Last reply 6 Jul 2021, 06:40
        0
        • O ODБOï
          6 Jul 2021, 01:56

          @Kyeiv hi you can simply set attached property "Layout.fillWidth" to true in on your dummy Item
          see also "preferredWidth"
          https://doc.qt.io/qt-5/qml-qtquick-layouts-layout.html#attached-properties

          K Offline
          K Offline
          Kyeiv
          wrote on 6 Jul 2021, 06:40 last edited by
          #3

          @ODБOï I am afraid it's not so simple because setting Item's Layout.fillWidth: true shifts everything to the right, so Text is not centered

          O 1 Reply Last reply 6 Jul 2021, 13:36
          0
          • K Kyeiv
            6 Jul 2021, 06:40

            @ODБOï I am afraid it's not so simple because setting Item's Layout.fillWidth: true shifts everything to the right, so Text is not centered

            O Offline
            O Offline
            ODБOï
            wrote on 6 Jul 2021, 13:36 last edited by ODБOï 7 Jun 2021, 13:44
            #4

            @Kyeiv said in Place items in GridLayout leaving empty column:

            it's not so simple

            how about using preferredWidth as i suggested ?

             GridLayout{
                        anchors.fill : parent
                        columns : 3
                            
                        Item{
                          Layout.fillWidth:true 
                          Layout.preferredWidth : 50
                           }
                        
                        Text{
                          text : "Hello"   
                          Layout.preferredWidth : 50
                          Layout.fillWidth:true
                        }
                        
                        Button{
                        }
                }
            

            another solution

                
                GridLayout{
                        anchors.fill : parent
                        columns : 3
                
                        Item{ 
                           Layout.fillWidth:true
                           Layout.columnSpan  : 2 
                            Text{
                               text : "Hello"   
                               anchors.centerIn:parent
                             }
                        }
                        
                        Button{
                        }
                }
            
            K 1 Reply Last reply 7 Jul 2021, 08:44
            0
            • O ODБOï
              6 Jul 2021, 13:36

              @Kyeiv said in Place items in GridLayout leaving empty column:

              it's not so simple

              how about using preferredWidth as i suggested ?

               GridLayout{
                          anchors.fill : parent
                          columns : 3
                              
                          Item{
                            Layout.fillWidth:true 
                            Layout.preferredWidth : 50
                             }
                          
                          Text{
                            text : "Hello"   
                            Layout.preferredWidth : 50
                            Layout.fillWidth:true
                          }
                          
                          Button{
                          }
                  }
              

              another solution

                  
                  GridLayout{
                          anchors.fill : parent
                          columns : 3
                  
                          Item{ 
                             Layout.fillWidth:true
                             Layout.columnSpan  : 2 
                              Text{
                                 text : "Hello"   
                                 anchors.centerIn:parent
                               }
                          }
                          
                          Button{
                          }
                  }
              
              K Offline
              K Offline
              Kyeiv
              wrote on 7 Jul 2021, 08:44 last edited by
              #5

              @ODБOï said in Place items in GridLayout leaving empty column:

              anchors.centerIn:parent

              unfortunately your solutions still shift the text to left or right.

              The only solution that i came up to and works but is ugly:

              GridLayout 
                      {
                          Layout.fillWidth: true
                          columns: 3
              
                          Item {
                              Layout.fillWidth: true                
                          }
                          Text
                          {
                              Layout.fillWidth: true
                              Layout.alignment: Qt.AlignHCenter
                              anchors.horizontalCenter: parent.horizontalCenter
                              horizontalAlignment: Text.AlignHCenter
                              text: "text"
                              color: "white"
                          }
                          Button
              	    {
                              Layout.alignment: Qt.AlignRight
                              implicitHeight: 35
                              Layout.maximumWidth: 35
              	    }
                      }
              
              O 1 Reply Last reply 7 Jul 2021, 08:52
              0
              • K Kyeiv
                7 Jul 2021, 08:44

                @ODБOï said in Place items in GridLayout leaving empty column:

                anchors.centerIn:parent

                unfortunately your solutions still shift the text to left or right.

                The only solution that i came up to and works but is ugly:

                GridLayout 
                        {
                            Layout.fillWidth: true
                            columns: 3
                
                            Item {
                                Layout.fillWidth: true                
                            }
                            Text
                            {
                                Layout.fillWidth: true
                                Layout.alignment: Qt.AlignHCenter
                                anchors.horizontalCenter: parent.horizontalCenter
                                horizontalAlignment: Text.AlignHCenter
                                text: "text"
                                color: "white"
                            }
                            Button
                	    {
                                Layout.alignment: Qt.AlignRight
                                implicitHeight: 35
                                Layout.maximumWidth: 35
                	    }
                        }
                
                O Offline
                O Offline
                ODБOï
                wrote on 7 Jul 2021, 08:52 last edited by
                #6

                @Kyeiv said in Place items in GridLayout leaving empty column:

                anchors.horizontalCenter: parent.horizontalCenter

                you can't use "anchors" inside an item that is direct child of a QML Layout, you should see a warning for that.

                This will put the text exactly in the center

                    GridLayout{
                            anchors.fill : parent
                            columns : 3   
                            Item{ 
                               Layout.fillWidth:true
                               Layout.columnSpan  : 3
                                Text{
                                   text : "Hello"   
                                   anchors.centerIn:parent
                                }
                                Button{
                                  anchors.right:parent.right
                                  anchors.verticalCenter:parent.verticalCenter
                                }
                            }
                    }
                
                1 Reply Last reply
                0

                4/6

                6 Jul 2021, 13:36

                • 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