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. Simple anchor bindings in QML

Simple anchor bindings in QML

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

    Hi all,

    mani.qml:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.5
    
    Window {
        visible: true
        width: 600; height: 400
        color: "linen"
    
        TLineEditV1 {
            id: input1
           // anchors.top: parent
            anchors.centerIn: parent
            text: "text 1"
        }
    }
    

    TLineEditV1:

    import QtQuick 2.12
    
    Rectangle {
        width: 96; height: input.height + 8
        color: "lightsteelblue"
        border.color: "gray"
    
       property alias text: input.text
    
        TextInput {
            id: input
            width: 100; height: 20
            anchors.fill: parent
            anchors.margins: 4
            focus: true
        }
    }
    

    The issue is that anchors.centerIn: parent in main.qml works fine but anchors.top: parent gives this error:

    Unable to assign QQuickRootItem to QQuickAnchorLine

    Why, please, and how to solve it?

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

      Hi @tomy , you need to write it like this:-

      anchors.top: parent.top
      
      • 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.top expects a QQuickAnchorLine, so i guess what it does is irrespective of height or width it positions itself with respect to the parent.

      Anchors(Examples):-

      anchors.top: parent.top
      anchors.left: parent.left
      anchors.right: parent.right
      anchors.bottom: parent.bottom
      

      Convenience Anchors(Examples):-

      anchors.fill: parent
      anchors.centerIn: parent
      

      I guess you can have a look into this post, i have explained a bit[https://forum.qt.io/topic/103522/anchor-loop-with-layout-centerin/3]

      Look into the documentation of anchors once[https://doc.qt.io/qt-5/qtquick-positioning-anchors.html]

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

      tomyT 1 Reply Last reply
      4
      • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

        Hi @tomy , you need to write it like this:-

        anchors.top: parent.top
        
        • 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.top expects a QQuickAnchorLine, so i guess what it does is irrespective of height or width it positions itself with respect to the parent.

        Anchors(Examples):-

        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        

        Convenience Anchors(Examples):-

        anchors.fill: parent
        anchors.centerIn: parent
        

        I guess you can have a look into this post, i have explained a bit[https://forum.qt.io/topic/103522/anchor-loop-with-layout-centerin/3]

        Look into the documentation of anchors once[https://doc.qt.io/qt-5/qtquick-positioning-anchors.html]

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

        @Shrinidhi-Upadhyaya

        anchors.top: parent.top

        What a silly mistake I made! I must have been out of my mind.
        Thanks

        Pradeep P NP 1 Reply Last reply
        1
        • tomyT tomy

          @Shrinidhi-Upadhyaya

          anchors.top: parent.top

          What a silly mistake I made! I must have been out of my mind.
          Thanks

          Pradeep P NP Offline
          Pradeep P NP Offline
          Pradeep P N
          wrote on last edited by
          #4

          Hi @tomy
          Also prefer grouping the properties in QML ( Just the best practice )

                  anchors {
                      top: parent.top;
                      left: parent.left;
                      right: parent.right;
                      bottom: parent.bottom;
                  }
          

          All the best.

          Pradeep Nimbalkar.
          Upvote the answer(s) that helped you to solve the issue...
          Keep code clean.

          1 Reply Last reply
          3

          • Login

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