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. Absolute QML newbie lost on alignment on anchors
Forum Updated to NodeBB v4.3 + New Features

Absolute QML newbie lost on alignment on anchors

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 3 Posters 1.8k Views 3 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.
  • idlefrogI Offline
    idlefrogI Offline
    idlefrog
    wrote last edited by
    #1

    I'm trying to do the business card example. Using anchors, I'm trying to show two rows of text, the 'name', and 'address'. But both are on top of each other:

    import QtQuick
    
    Window {
        width: height*1.586
        height: 480
    
        visible: true
        title: qsTr("Business Card")
    
        component ContactInfo: QtObject {
            property string name
            property string address
        }
    
        ContactInfo {
            id: myContactInfo
            name: "Ms Just Lost"
            address: "Candy Cane Lane"
        }
    
        Rectangle{
            id: name
            anchors.top: parent.top
            anchors.left: parent.left
            Text{
                text: myContactInfo.name
                font.pointSize: 30
            }
    
        }
        Rectangle{
            id:address
            anchors.top: name.bottom
            anchors.left: name.left
            Text{
                text: myContactInfo.address
                font.pointSize: 15
            }
        }
    }
    

    Why does this not work?
    The Rectangle 'name' is in position top left of the parent.
    The Rectangle 'address' be below the 'name' rectangle.
    Guess I'm looking for some hints rather than any given solution.
    Why is this wrong?

    jeremy_kJ 1 Reply Last reply
    0
    • idlefrogI idlefrog

      @jeremy_k Oh, so you are saying the Rectangle does not implicitly take the size of the Text!
      Hence, the parent Rectangle should take the size of the text explicitly?

      (I did quickly look at the solution, but that didn't explain to me why my micro attempt was off the beaten track. :( )

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote last edited by
      #4

      @idlefrog said in Absolute QML newbie lost on alignment on anchors:

      @jeremy_k Oh, so you are saying the Rectangle does not implicitly take the size of the Text!
      Hence, the parent Rectangle should take the size of the text explicitly?

      That's right. You can confirm by adding this to your Window, then look at your application output when you run it:

      Component.onCompleted: {
          console.log("Name width:", name.width)
          console.log("Name height:", name.height)
      }
      

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      idlefrogI 1 Reply Last reply
      1
      • idlefrogI idlefrog

        I'm trying to do the business card example. Using anchors, I'm trying to show two rows of text, the 'name', and 'address'. But both are on top of each other:

        import QtQuick
        
        Window {
            width: height*1.586
            height: 480
        
            visible: true
            title: qsTr("Business Card")
        
            component ContactInfo: QtObject {
                property string name
                property string address
            }
        
            ContactInfo {
                id: myContactInfo
                name: "Ms Just Lost"
                address: "Candy Cane Lane"
            }
        
            Rectangle{
                id: name
                anchors.top: parent.top
                anchors.left: parent.left
                Text{
                    text: myContactInfo.name
                    font.pointSize: 30
                }
        
            }
            Rectangle{
                id:address
                anchors.top: name.bottom
                anchors.left: name.left
                Text{
                    text: myContactInfo.address
                    font.pointSize: 15
                }
            }
        }
        

        Why does this not work?
        The Rectangle 'name' is in position top left of the parent.
        The Rectangle 'address' be below the 'name' rectangle.
        Guess I'm looking for some hints rather than any given solution.
        Why is this wrong?

        jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote last edited by
        #2

        @idlefrog said in Absolute QML newbie lost on alignment on anchors:

        Guess I'm looking for some hints rather than any given solution.

        What is the size of the name Rectangle, and what is the coordinate if its bottom left corner?

        Asking a question about code? http://eel.is/iso-c++/testcase/

        idlefrogI 1 Reply Last reply
        2
        • jeremy_kJ jeremy_k

          @idlefrog said in Absolute QML newbie lost on alignment on anchors:

          Guess I'm looking for some hints rather than any given solution.

          What is the size of the name Rectangle, and what is the coordinate if its bottom left corner?

          idlefrogI Offline
          idlefrogI Offline
          idlefrog
          wrote last edited by
          #3

          @jeremy_k Oh, so you are saying the Rectangle does not implicitly take the size of the Text!
          Hence, the parent Rectangle should take the size of the text explicitly?

          (I did quickly look at the solution, but that didn't explain to me why my micro attempt was off the beaten track. :( )

          JKSHJ jeremy_kJ 2 Replies Last reply
          0
          • idlefrogI idlefrog

            @jeremy_k Oh, so you are saying the Rectangle does not implicitly take the size of the Text!
            Hence, the parent Rectangle should take the size of the text explicitly?

            (I did quickly look at the solution, but that didn't explain to me why my micro attempt was off the beaten track. :( )

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote last edited by
            #4

            @idlefrog said in Absolute QML newbie lost on alignment on anchors:

            @jeremy_k Oh, so you are saying the Rectangle does not implicitly take the size of the Text!
            Hence, the parent Rectangle should take the size of the text explicitly?

            That's right. You can confirm by adding this to your Window, then look at your application output when you run it:

            Component.onCompleted: {
                console.log("Name width:", name.width)
                console.log("Name height:", name.height)
            }
            

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            idlefrogI 1 Reply Last reply
            1
            • idlefrogI idlefrog

              @jeremy_k Oh, so you are saying the Rectangle does not implicitly take the size of the Text!
              Hence, the parent Rectangle should take the size of the text explicitly?

              (I did quickly look at the solution, but that didn't explain to me why my micro attempt was off the beaten track. :( )

              jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote last edited by
              #5

              @idlefrog said in Absolute QML newbie lost on alignment on anchors:

              Hence, the parent Rectangle should take the size of the text explicitly?

              Depending on the use case, possibly. Some padding is probably desirable for legibility and aesthetic concerns. It might also be desirable to force the child Text to fit within a parent and resort to eliding.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              0
              • JKSHJ JKSH

                @idlefrog said in Absolute QML newbie lost on alignment on anchors:

                @jeremy_k Oh, so you are saying the Rectangle does not implicitly take the size of the Text!
                Hence, the parent Rectangle should take the size of the text explicitly?

                That's right. You can confirm by adding this to your Window, then look at your application output when you run it:

                Component.onCompleted: {
                    console.log("Name width:", name.width)
                    console.log("Name height:", name.height)
                }
                
                idlefrogI Offline
                idlefrogI Offline
                idlefrog
                wrote last edited by
                #6

                @JKSH said in Absolute QML newbie lost on alignment on anchors:

                Component.onCompleted: {
                console.log("Name width:", name.width)
                console.log("Name height:", name.height)
                }

                That's very useful! It helped me a lot. Thank you!

                1 Reply Last reply
                0
                • idlefrogI Offline
                  idlefrogI Offline
                  idlefrog
                  wrote last edited by
                  #7

                  Thank you for all your hint/suggestions. Just to get things aligned, I did this:

                   Rectangle{
                          id: name
                          anchors.top: parent.top
                          anchors.left: parent.left
                          width: name_text.width
                          height: name_text.height
                          color: "blue"
                          Text{
                              id: name_text
                              text: myContactInfo.name
                              font.pointSize: 30
                          }
                  
                      }
                      Rectangle{
                          id:address
                          anchors.top: name.bottom
                          anchors.left: name.left
                          width: address_text.width
                          height: address_text.height
                          color: "green"
                          Text{
                              id:address_text
                              text: myContactInfo.address
                              font.pointSize: 15
                          }
                      }
                  

                  So I gave both Rectangles a reference to the child Text items.
                  Thanks guys!

                  1 Reply Last reply
                  0
                  • idlefrogI idlefrog has marked this topic as solved
                  • JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote last edited by
                    #8

                    You're welcome! Thank you for sharing your journey and your solution.

                    Here are some additional tidbits that might be of interest to you:

                    Anchor default values

                    Items are always placed at their parent's top-left by default. So, you can omit these lines and still get the same result:

                    anchors.top: parent.top
                    anchors.left: parent.left
                    

                    Having said that, you can also keep them for clarity.

                    Label vs. Text

                    Displaying text inside a coloured rectangle is a very common requirement. You can simplify your code by using Label instead (https://doc.qt.io/qt-6/qml-qtquick-controls-label.html ). Add import QtQuick.Controls.Basic and try the following:

                    Label {
                        id: name
                        background: Rectangle { color: "blue" }
                        text: myContactInfo.name
                        font.pointSize: 30
                    }
                    Label {
                        id: address
                        anchors.top: name.bottom
                        background: Rectangle { color: "green" }
                        text: myContactInfo.address
                        font.pointSize: 15
                    }
                    

                    Note: One difference is that Label might change your font colour based on your OS settings (e.g. dark mode)

                    Positioners vs. Anchors

                    See https://doc.qt.io/qt-6/qtquick-positioning-layouts.html -- instead of anchoring your 2nd label to the 1st label, you could put them inside a Column:

                    Column {
                        // Quiz (open-book): What is the width and height of this Column?
                    
                        Label {
                            id: name
                            background: Rectangle { color: "blue" }
                            text: myContactInfo.name
                            font.pointSize: 30
                        }
                        Label {
                            id: address
                            background: Rectangle { color: "green" }
                            text: myContactInfo.address
                            font.pointSize: 15
                        }
                    }
                    

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    2

                    • Login

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