Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Calculate width of QML Item

Calculate width of QML Item

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 3.2k 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.
  • JoeCFDJ Offline
    JoeCFDJ Offline
    JoeCFD
    wrote on last edited by
    #2

    add a horizontal layout to put two images and text in the middle.

    1 Reply Last reply
    1
    • S Offline
      S Offline
      Stefanoxjx
      wrote on last edited by
      #3

      Hi joeCFD, and thanks for yout help.
      I'm trying to understand RowLayout but (grrrrrrrr !#@?) I don't understand :(
      From code below, I expect that all icons was equally distributed in the red rectangle, instead the result is this:
      a118beb2-e527-421e-b64a-608969c41eec-image.png
      What I wrong?

      import QtQuick 2.15
      import QtQuick.Window 2.15
      import QtQuick.Layouts 1.3
      
      Window {
          width: 1024
          height: 800
          visible: true
          title: qsTr("Hello World")
      
          Rectangle {
              id: menuBar
              width: parent.width
              height: 100
              color: "grey"
      
              Rectangle /*Item*/ {
                  id: centralMenu
      
                  anchors {
                      top: parent.top
                      bottom: parent.bottom
                      horizontalCenter: parent.horizontalCenter
                  }
                  color: "red"
                  width: parent.width * 0.6
      
                  RowLayout {
                      anchors {
                          fill: parent
                          topMargin: 10
                          bottomMargin: 10
                      }
      
                      //spacing: 2
      
                      Image {
                          Layout.fillHeight: true
                          fillMode: Image.PreserveAspectFit
                          source: "Images/HAL9000.svg"
                      }
      
                      Image {
                          Layout.fillHeight: true
                          fillMode: Image.PreserveAspectFit
                          source: "Images/Stats.png"
                      }
      
                      Image {
                          Layout.fillHeight: true
                          fillMode: Image.PreserveAspectFit
                          source: "Images/Sentinel.png"
                      }
      
                      Image {
                          Layout.fillHeight: true
                          fillMode: Image.PreserveAspectFit
                          source: "Images/bluetooth.pnt"
                      }
      
                      Image {
                          Layout.fillHeight: true
                          fillMode: Image.PreserveAspectFit
                          source: "Images/Option.png"
                      }
      
                      Image {
                          Layout.fillHeight: true
                          fillMode: Image.PreserveAspectFit
                          source: "Images/Multimedia.png"
                      }
      
                      Image {
                          Layout.fillHeight: true
                          fillMode: Image.PreserveAspectFit
                          source: "Images/joystick.png"
                      }
                  }
              }
          }
      }
      
      1 Reply Last reply
      0
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #4

        @Stefanoxjx said in Calculate width of QML Item:

                RowLayout {
                    anchors {
                        fill: parent
                        topMargin: 10
                        bottomMargin: 10
                    }
        
                    //spacing: 2
        
                    Image {
                        Layout.fillHeight: true
                        fillMode: Image.PreserveAspectFit
                        source: "Images/HAL9000.svg"
                    }
        
                    Image {
                        Layout.fillHeight: true
                        fillMode: Image.PreserveAspectFit
                        source: "Images/Stats.png"
                    }
        
                    Image {
                        Layout.fillHeight: true
                        fillMode: Image.PreserveAspectFit
                        source: "Images/Sentinel.png"
                    }
        
                    Image {
                        Layout.fillHeight: true
                        fillMode: Image.PreserveAspectFit
                        source: "Images/bluetooth.pnt"
                    }
        
                    Image {
                        Layout.fillHeight: true
                        fillMode: Image.PreserveAspectFit
                        source: "Images/Option.png"
                    }
        
                    Image {
                        Layout.fillHeight: true
                        fillMode: Image.PreserveAspectFit
                        source: "Images/Multimedia.png"
                    }
        
                    Image {
                        Layout.fillHeight: true
                        fillMode: Image.PreserveAspectFit
                        source: "Images/joystick.png"
                    }
                }
        

        Try this and you may need spacing.

                    RowLayout {
                        anchors {
                            fill: parent
                            topMargin: 10
                            bottomMargin: 10
                        }
        
                        //spacing: 2
                        Item {
                                    Layout.fillHeight: true
                                    Layout.fillWidth: true
                        }
        
                        Image {
                            Layout.fillHeight: true
                            fillMode: Image.PreserveAspectFit
                            source: "Images/HAL9000.svg"
                        }
        
                        Image {
                            Layout.fillHeight: true
                            fillMode: Image.PreserveAspectFit
                            source: "Images/Stats.png"
                        }
        
                        Image {
                            Layout.fillHeight: true
                            fillMode: Image.PreserveAspectFit
                            source: "Images/Sentinel.png"
                        }
        
                        Image {
                            Layout.fillHeight: true
                            fillMode: Image.PreserveAspectFit
                            source: "Images/bluetooth.pnt"
                        }
        
                        Image {
                            Layout.fillHeight: true
                            fillMode: Image.PreserveAspectFit
                            source: "Images/Option.png"
                        }
        
                        Image {
                            Layout.fillHeight: true
                            fillMode: Image.PreserveAspectFit
                            source: "Images/Multimedia.png"
                        }
        
                        Image {
                            Layout.fillHeight: true
                            fillMode: Image.PreserveAspectFit
                            source: "Images/joystick.png"
                        }       
        
                        Item {
                                    Layout.fillHeight: true
                                    Layout.fillWidth: true
                        }
                    }
        
        
        1 Reply Last reply
        1
        • S Offline
          S Offline
          Stefanoxjx
          wrote on last edited by Stefanoxjx
          #5

          I try this, but the result is the same :(

          import QtQuick 2.15
          import QtQuick.Window 2.15
          import QtQuick.Layouts 1.3
          
          Window {
              width: 1024
              height: 800
              visible: true
              title: qsTr("Hello World")
              
              Rectangle {
                  id: menuBar
                  width: parent.width
                  height: 100
                  color: "grey"
                  
                  Rectangle /*Item*/ {
                      id: centralMenu
                      
                      anchors {
                          top: parent.top
                          bottom: parent.bottom
                          horizontalCenter: parent.horizontalCenter
                      }
                      color: "red"
                      width: parent.width * 0.6
                      
                      RowLayout {
                          anchors {
                              fill: parent
                              topMargin: 10
                              bottomMargin: 10
                          }
                          
                          spacing: 2
                          Item {
                              Layout.fillHeight: true
                              Layout.fillWidth: true
                          }
                          
                          Image {
                              Layout.fillHeight: true
                              fillMode: Image.PreserveAspectFit
                              source: "Images/HAL9000.svg"
                          }
                          
                          Image {
                              Layout.fillHeight: true
                              fillMode: Image.PreserveAspectFit
                              source: "Images/Stats.png"
                          }
                          
                          Image {
                              Layout.fillHeight: true
                              fillMode: Image.PreserveAspectFit
                              source: "Images/Sentinel.png"
                          }
                          
                          Image {
                              Layout.fillHeight: true
                              fillMode: Image.PreserveAspectFit
                              source: "Images/bluetooth.pnt"
                          }
                          
                          Image {
                              Layout.fillHeight: true
                              fillMode: Image.PreserveAspectFit
                              source: "Images/Option.png"
                          }
                          
                          Image {
                              Layout.fillHeight: true
                              fillMode: Image.PreserveAspectFit
                              source: "Images/Multimedia.png"
                          }
                          
                          Image {
                              Layout.fillHeight: true
                              fillMode: Image.PreserveAspectFit
                              source: "Images/joystick.png"
                          }
                          
                          Item {
                              Layout.fillHeight: true
                              Layout.fillWidth: true
                          }
                      }
                      
                  }
              }
          }
          
          1 Reply Last reply
          0
          • JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by
            #6

            add this to each image
            Layout.alignment: Qt.AlignCenter

            1 Reply Last reply
            1
            • S Offline
              S Offline
              Stefanoxjx
              wrote on last edited by
              #7

              @JoeCFD said in Calculate width of QML Item:

              Layout.alignment: Qt.AlignCenter

              Doesn't work :(

              import QtQuick 2.15
              import QtQuick.Window 2.15
              import QtQuick.Layouts 1.3
              
              Window {
                  width: 1024
                  height: 800
                  visible: true
                  title: qsTr("Hello World")
              
                  Rectangle {
                      id: menuBar
                      width: parent.width
                      height: 100
                      color: "grey"
              
                      Rectangle /*Item*/ {
                          id: centralMenu
              
                          anchors {
                              top: parent.top
                              bottom: parent.bottom
                              horizontalCenter: parent.horizontalCenter
                          }
                          color: "red"
                          width: parent.width * 0.6
              
                          RowLayout {
                              anchors {
                                  fill: parent
                                  topMargin: 10
                                  bottomMargin: 10
                              }
              
                              //spacing: 2
                              Item {
                                  Layout.fillHeight: true
                                  Layout.fillWidth: true
                              }
              
                              Image {
                                  Layout.fillHeight: true
                                  Layout.alignment: Qt.AlignCenter
                                  fillMode: Image.PreserveAspectFit
                                  source: "Images/HAL9000.svg"
                              }
              
                              Image {
                                  Layout.fillHeight: true
                                  Layout.alignment: Qt.AlignCenter
                                  fillMode: Image.PreserveAspectFit
                                  source: "Images/Stats.png"
                              }
              
                              Image {
                                  Layout.fillHeight: true
                                  Layout.alignment: Qt.AlignCenter
                                  fillMode: Image.PreserveAspectFit
                                  source: "Images/Sentinel.png"
                              }
              
                              Image {
                                  Layout.fillHeight: true
                                  Layout.alignment: Qt.AlignCenter
                                  fillMode: Image.PreserveAspectFit
                                  source: "Images/bluetooth.png"
                              }
              
                              Image {
                                  Layout.fillHeight: true
                                  Layout.alignment: Qt.AlignCenter
                                  fillMode: Image.PreserveAspectFit
                                  source: "Images/Option.png"
                              }
              
                              Image {
                                  Layout.fillHeight: true
                                  Layout.alignment: Qt.AlignCenter
                                  fillMode: Image.PreserveAspectFit
                                  source: "Images/Multimedia.png"
                              }
              
                              Image {
                                  Layout.fillHeight: true
                                  Layout.alignment: Qt.AlignCenter
                                  fillMode: Image.PreserveAspectFit
                                  source: "Images/joystick.png"
                              }
              
                              Item {
                                  Layout.fillHeight: true
                                  Layout.fillWidth: true
                              }
                          }
                      }
                  }
              }
              
              1 Reply Last reply
              0
              • JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by JoeCFD
                #8

                @Stefanoxjx said in Calculate width of QML Item:

                import QtQuick.Layouts 1.3

                import QtQuick.Layouts 1.15. Use two images first to see if they are in the center.

                1 Reply Last reply
                1
                • S Offline
                  S Offline
                  Stefanoxjx
                  wrote on last edited by
                  #9

                  This is the result with two images and Import 1.15 instead 1.3:

                  009c212b-05d9-4e87-9a57-7549ab886b53-image.png

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Stefanoxjx
                    wrote on last edited by
                    #10

                    I discovered that for Layout is better use Layout.preferredWidth and Layout.preferredHeight and the empty initial and endig Item is not necessary.

                    37432d0c-c980-48e0-9ccf-c8c96734224e-image.png

                    Many thanks for your help :)

                    JoeCFDJ 1 Reply Last reply
                    0
                    • S Stefanoxjx

                      I discovered that for Layout is better use Layout.preferredWidth and Layout.preferredHeight and the empty initial and endig Item is not necessary.

                      37432d0c-c980-48e0-9ccf-c8c96734224e-image.png

                      Many thanks for your help :)

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by JoeCFD
                      #11

                      @Stefanoxjx Great you made it. The empty item is supposed to work as spacer. You may need it down the road.

                      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