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. Anchor Loop with Layout.centerIn?
Forum Updated to NodeBB v4.3 + New Features

Anchor Loop with Layout.centerIn?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.6k Views 2 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    I have this code:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Layouts 1.12
    import QtQuick.Controls 2.5
    
    ApplicationWindow {
        id: mainwin
        visible: true
        width: 1920/2
        height: 1080/2
    
        Rectangle {
            id: meters1
            width: parent.width
            height: parent.height/2
    
            color: "red"
    
            RowLayout {
                spacing: 5
                anchors.centerIn: parent
    
                Rectangle {
                    Layout.minimumHeight: 50 //meters1.height
                    Layout.minimumWidth: height*2
                    color: "darkgrey"
    
                    Text {
                        anchors.centerIn: parent
                        text: "meter1"
                    }
                }
                Rectangle {
                    Layout.minimumHeight: 50 //meters1.height
                    Layout.minimumWidth: height*2
                    color: "grey"
    
                    Text {
                        anchors.centerIn: parent
                        text: "meter2"
                    }
                }
            }
        }
    }
    

    It gives me this error:

    qrc:/main_center.qml:19:9: QML RowLayout: Possible anchor loop detected on centerIn.
    

    I don't understand where the loop could be. Is this a real error?

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      @fcarney said in Anchor Loop with Layout.centerIn?:

      I don't understand where the loop could be.

      I think its a bug on loop detection or centerIn works differently than I think it does.
      I can make the error go away by replacing "anchors.centerIn: parent" with this:

      anchors.horizontalCenter: parent.horizontalCenter
      anchors.verticalCenter: parent.verticalCenter
      

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0
      • Shrinidhi UpadhyayaS Offline
        Shrinidhi UpadhyayaS Offline
        Shrinidhi Upadhyaya
        wrote on last edited by
        #3

        Hi @fcarney , just to add to it

        • First thing is anchors.centerIn is a convenience anchor.

        • anchors.centerIn expects a QQuickItem which means that, it should have some height or width, so based upon the height or width it anchors it, while anchors.horizontalCenter expects a QQuickAnchorLine, so i guess what it does is irrespective of height or width it positions itself with respect to the parent.

        So now for example if you give like this in your code:-

        anchors.centerIn: parent.horizontalCenter
        

        It will give you a error like this:-

        0_1559627496313_46c0195d-b836-42ea-8741-72708e9d99ff-image.png

        • And as i see in your code you have not given height and width to the RowLayout, you need to specify the height and width to it, the reason why the 2 Rectangles are taking the height and width now is because you have hardcoded the width and height.

        So for example just remove the Text inside the 2 rectangles, you will have the below output:-

        0_1559627968688_0c47e8de-450e-4804-b0c3-a4553ad684a6-image.png

        But if you replace:-

         //                Layout.minimumHeight: 50 //meters1.height
        //                Layout.minimumWidth: height*2
                    Layout.fillHeight: true
                    Layout.fillWidth: true
        

        Then in the output nothing will be visible because the RowLayout does not have any height and width, but if you give height and width to the RowLayout, you will have this:-

        0_1559628241421_822b9163-d547-4e25-8ae7-37b1dbcff984-image.png

        Note:- Please do correct me if iam wrong somewhere, lets all learn together cheers :-)

        Shrinidhi Upadhyaya.
        Upvote the answer(s) that helped you to solve the issue.

        1 Reply Last reply
        4
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #4

          @Shrinidhi-Upadhyaya said in Anchor Loop with Layout.centerIn?:

          Then in the output nothing will be visible because the RowLayout does not have any height and width, but if you give height and width to the RowLayout,

          I think you are right. I ended up switching to a Row component and I gave it an "anchors.fill: parent" and also the:

          anchors.horizontalCenter: parent.horizontalCenter
          anchors.verticalCenter: parent.verticalCenter
          

          So now it behaves better.
          I was not sure if a RowLayout could be given dimensions as a layout I thought depended upon is children to determine its size. That might be where the loop came in. It needs the size of the children to determine where to place the component to center it.

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          1

          • Login

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