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. Dealing with a Button and anchors

Dealing with a Button and anchors

Scheduled Pinned Locked Moved Solved QML and Qt Quick
10 Posts 3 Posters 1.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.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by
    #1

    Hi all,

    Why do anchors in this QML code not work, please?!

    Window {
        id: window
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Button {
            id: myButton
            anchors.bottom: window.bottom
            anchors.right: window.right
    
            text:"<font color='green'>" + qsTr("OK") + "</font>"
            font.bold: true
            font.pixelSize: window.width / 50
            implicitWidth: window.width / 7; implicitHeight: window.height / 14
    
            background: Rectangle { color: "lightGrey"; radius: 20 }
    
        }
    }
    
    1 Reply Last reply
    0
    • G Offline
      G Offline
      GrahamLa
      wrote on last edited by
      #2

      Hi
      I think this is because the Window QML element does not de``rive from 'Item'.
      If you anchor like this -

              anchors.bottom: parent.bottom
              anchors.right: parent.right
      

      Then you will get want you want.

      tomyT 1 Reply Last reply
      1
      • G GrahamLa

        Hi
        I think this is because the Window QML element does not de``rive from 'Item'.
        If you anchor like this -

                anchors.bottom: parent.bottom
                anchors.right: parent.right
        

        Then you will get want you want.

        tomyT Offline
        tomyT Offline
        tomy
        wrote on last edited by
        #3

        @GrahamLa

        In this code either:

        import QtQuick 2.12
        import QtQuick.Window 2.12
        import QtQuick.Controls 2.5
        
        Window {
            id: window
            visible: true
            width: 640
            height: 480
            title: qsTr("Hello World")
        
            Rectangle {
                id: widerArea
                color: "cornflowerblue"
                width: window.width / 1.2; height: window.height / 1.2
                anchors.centerIn: window
        
                Button {
                    id: myButton
                    anchors.bottom: window.bottom
                    anchors.right: window.right
        
                    text:"<font color='green'>" + qsTr("OK") + "</font>"
                    font.bold: true
                    font.pixelSize: window.width / 50
                    implicitWidth: window.width / 7; implicitHeight: window.height / 14
        
                    background: Rectangle { color: "lightGrey"; radius: 20 }
        
                }
            }
        }
        

        I get an error for the line:anchors.centerIn: window. Is it too because of the reason you mentioned, please?

        G KroMignonK 2 Replies Last reply
        0
        • tomyT tomy

          @GrahamLa

          In this code either:

          import QtQuick 2.12
          import QtQuick.Window 2.12
          import QtQuick.Controls 2.5
          
          Window {
              id: window
              visible: true
              width: 640
              height: 480
              title: qsTr("Hello World")
          
              Rectangle {
                  id: widerArea
                  color: "cornflowerblue"
                  width: window.width / 1.2; height: window.height / 1.2
                  anchors.centerIn: window
          
                  Button {
                      id: myButton
                      anchors.bottom: window.bottom
                      anchors.right: window.right
          
                      text:"<font color='green'>" + qsTr("OK") + "</font>"
                      font.bold: true
                      font.pixelSize: window.width / 50
                      implicitWidth: window.width / 7; implicitHeight: window.height / 14
          
                      background: Rectangle { color: "lightGrey"; radius: 20 }
          
                  }
              }
          }
          

          I get an error for the line:anchors.centerIn: window. Is it too because of the reason you mentioned, please?

          G Offline
          G Offline
          GrahamLa
          wrote on last edited by
          #4

          @tomy said in Dealing with a Button and anchors:

          Window {
          id: window
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")

          Rectangle {
              id: widerArea
              color: "cornflowerblue"
              width: window.width / 1.2; height: window.height / 1.2
              anchors.centerIn: window
          
              Button {
                  id: myButton
                  anchors.bottom: window.bottom
                  anchors.right: window.right
          
                  text:"<font color='green'>" + qsTr("OK") + "</font>"
                  font.bold: true
                  font.pixelSize: window.width / 50
                  implicitWidth: window.width / 7; implicitHeight: window.height / 14
          
                  background: Rectangle { color: "lightGrey"; radius: 20 }
          
              }
          }
          

          }

          Yes, just use parent

          1 Reply Last reply
          0
          • tomyT tomy

            @GrahamLa

            In this code either:

            import QtQuick 2.12
            import QtQuick.Window 2.12
            import QtQuick.Controls 2.5
            
            Window {
                id: window
                visible: true
                width: 640
                height: 480
                title: qsTr("Hello World")
            
                Rectangle {
                    id: widerArea
                    color: "cornflowerblue"
                    width: window.width / 1.2; height: window.height / 1.2
                    anchors.centerIn: window
            
                    Button {
                        id: myButton
                        anchors.bottom: window.bottom
                        anchors.right: window.right
            
                        text:"<font color='green'>" + qsTr("OK") + "</font>"
                        font.bold: true
                        font.pixelSize: window.width / 50
                        implicitWidth: window.width / 7; implicitHeight: window.height / 14
            
                        background: Rectangle { color: "lightGrey"; radius: 20 }
            
                    }
                }
            }
            

            I get an error for the line:anchors.centerIn: window. Is it too because of the reason you mentioned, please?

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #5

            @tomy said in Dealing with a Button and anchors:

            I get an error for the line:anchors.centerIn: window. Is it too because of the reason you mentioned, please?

            The problem is that Windows does not derive from Item, so it do not have an anchros property (cf. https://doc.qt.io/qt-5/qml-qtquick-window-window.html)

            To solve this, you have to use parent.
            So why does using parent? Because the children you define in a Window become children of an intermediate Item called contentItem. Which will have an anchors propertie.

            Hope this helps.

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            4
            • tomyT Offline
              tomyT Offline
              tomy
              wrote on last edited by
              #6

              Thanks to all.
              In my second code above I wanted to put the button in the bottom-right corner of the window not widerArea!
              For that I think, the button ought to be declared outside the widerArea and use parent there.

              KroMignonK 1 Reply Last reply
              0
              • tomyT tomy

                Thanks to all.
                In my second code above I wanted to put the button in the bottom-right corner of the window not widerArea!
                For that I think, the button ought to be declared outside the widerArea and use parent there.

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #7

                @tomy said in Dealing with a Button and anchors:

                In my second code above I wanted to put the button in the bottom-right corner of the window not widerArea!

                Should be quite easy:

                Window {
                    id: window
                    visible: true
                    width: 640
                    height: 480
                    title: qsTr("Hello World")
                
                    Rectangle {
                        id: widerArea
                        color: "cornflowerblue"
                        width: window.width / 1.2; height: window.height / 1.2
                        anchors.centerIn: parent
                    }
                    Button {
                        id: myButton
                        anchors.bottom: parent.bottom
                        anchors.right: parent.right
                
                        text:"<font color='green'>" + qsTr("OK") + "</font>"
                        font.bold: true
                        font.pixelSize: window.width / 50
                        implicitWidth: window.width / 7; implicitHeight: window.height / 14
                
                        background: Rectangle { color: "lightGrey"; radius: 20 }
                    }
                }
                
                

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                tomyT 1 Reply Last reply
                0
                • KroMignonK KroMignon

                  @tomy said in Dealing with a Button and anchors:

                  In my second code above I wanted to put the button in the bottom-right corner of the window not widerArea!

                  Should be quite easy:

                  Window {
                      id: window
                      visible: true
                      width: 640
                      height: 480
                      title: qsTr("Hello World")
                  
                      Rectangle {
                          id: widerArea
                          color: "cornflowerblue"
                          width: window.width / 1.2; height: window.height / 1.2
                          anchors.centerIn: parent
                      }
                      Button {
                          id: myButton
                          anchors.bottom: parent.bottom
                          anchors.right: parent.right
                  
                          text:"<font color='green'>" + qsTr("OK") + "</font>"
                          font.bold: true
                          font.pixelSize: window.width / 50
                          implicitWidth: window.width / 7; implicitHeight: window.height / 14
                  
                          background: Rectangle { color: "lightGrey"; radius: 20 }
                      }
                  }
                  
                  
                  tomyT Offline
                  tomyT Offline
                  tomy
                  wrote on last edited by tomy
                  #8

                  @KroMignon
                  This way, the button will be put in the bottom-right corner of the rectangle not the window, because the parent of mybutton is widerArea not window.

                  Still think the button ought to be declared out of widerArea for that purpose. And yes, it's quite easy. :)

                  KroMignonK 1 Reply Last reply
                  0
                  • tomyT tomy

                    @KroMignon
                    This way, the button will be put in the bottom-right corner of the rectangle not the window, because the parent of mybutton is widerArea not window.

                    Still think the button ought to be declared out of widerArea for that purpose. And yes, it's quite easy. :)

                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by
                    #9

                    @tomy said in Dealing with a Button and anchors:

                    This way, the button will be put in the bottom-right corner of the rectangle not the window, because the parent of mybutton is widerArea not window.

                    Nope, parent of myButton is window not widerArea, why do you think it is not the case?

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    tomyT 1 Reply Last reply
                    0
                    • KroMignonK KroMignon

                      @tomy said in Dealing with a Button and anchors:

                      This way, the button will be put in the bottom-right corner of the rectangle not the window, because the parent of mybutton is widerArea not window.

                      Nope, parent of myButton is window not widerArea, why do you think it is not the case?

                      tomyT Offline
                      tomyT Offline
                      tomy
                      wrote on last edited by
                      #10

                      @KroMignon
                      Yeah, right. I didn't see that close curly bracket! :)

                      anchors.centerIn: parent
                          }     <= This
                          Button {
                              id: myButton
                      
                      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