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.
  • S Offline
    S Offline
    Stefanoxjx
    wrote on last edited by
    #1

    Hi guys,
    can you help me to how calculate a QML Item width?
    Wrongly, I thought that the width was automatically calculated, but seems not be!

    I've this Item that I want to center in a Rectangle:

    import QtQuick 2.15
    import QtQuick.Window 2.15
    
    Rectangle /*Item*/ {
        id: tempSelector
    
        color: "orange"
    //anchors.fill: parent
    
    //    height: parent.height
    width: 120
        //width: parent.width
    height: parent.height
        Image {
            id: tempDown
    
            anchors {
                left: parent.left
                verticalCenter: parent.verticalCenter
            }
    
            height: parent.height / 4
            fillMode: Image.PreserveAspectFit
            source: "Images/TempDown.png"
        }
    
        Text {
            id: temp
    
            anchors {
                verticalCenter: parent.verticalCenter
                left: tempDown.right
                leftMargin: 10
            }
    
            font.pixelSize: 35
            color: "white"
            text: "20.0"
        }
    
        Image {
            id: tempUp
    
            anchors {
                verticalCenter: tempDown.verticalCenter
                left: temp.right
                leftMargin: 10
            }
    
            height: parent.height / 4
            fillMode: Image.PreserveAspectFit
            source: "Images/TempUp.png"
        }
    }
    

    but if I specify width: 120, I've this result:
    76671b2f-0f42-427b-96ee-09906390c2db-image.png

    if I don't specify the width property, this is the result:
    cd2a0715-27c0-4195-87e1-99403738005a-image.png

    With width: 120 I solve the problem, but is bad solution, because the temperature could be 0.0 (lenght minor of 120) and the centering could be inaccurate.

    Which solution I could use for this problem?
    Thanks.

    1 Reply Last reply
    0
    • 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