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

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 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?

    ODБOïO 1 Reply Last reply
    0
    • K Kyeiv

      @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
      	    }
              }
      
      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on 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

        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?

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on 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
        0
        • ODБOïO ODБOï

          @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 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

          ODБOïO 1 Reply Last reply
          0
          • K Kyeiv

            @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

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by ODБOï
            #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
            0
            • ODБOïO ODБOï

              @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 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
              	    }
                      }
              
              ODБOïO 1 Reply Last reply
              0
              • K Kyeiv

                @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
                	    }
                        }
                
                ODБOïO Offline
                ODБOïO Offline
                ODБOï
                wrote on 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

                • Login

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