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. Access anchors.centerIn programatically
Qt 6.11 is out! See what's new in the release blog

Access anchors.centerIn programatically

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 2.0k 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.
  • S Offline
    S Offline
    SquimbellyToes
    wrote on last edited by
    #1

    Hello Folks;

    I'm trying to do the following in a C++ class in which I programmatically create QQuickItems:

    @
    QQmlComponent vertexComponent(_engine, "../Resources/qml/MyItem.qml");
    QObject *vObject = vertexComponent.create();
    QQuickItem vQI = qobject_cast<QQuickItem>(vObject);
    vQI->setProperty("anchors.centerIn", "parent");
    ...
    @

    However, I'm finding that I'm unable to set the anchors.centerIn property in this manner. In fact, I can find no way to access and set the anchors value through QQuickItem or the QMetaObject, as they seem to be private.

    Might anyone know how I might be able to set anchors values from C++?

    Thank you very much,
    S.T.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      PhTe
      wrote on last edited by
      #2

      I have the same problem now. Does anyone has a solution for it?

      p3c0P 1 Reply Last reply
      0
      • P PhTe

        I have the same problem now. Does anyone has a solution for it?

        p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by p3c0
        #3

        @PhTe I too had a similar requirement and ended up doing one of the following 2 ways

        • Setting the anchors.centerIn: parent in the QML to be loaded itself. Make sure parent is set using setParentItem
          (See it here and here)
        • Setting x and y properties after item creation. x and y can be calculated using its width and height according to parent's width and height.
        QQuickItem *parent = view.rootObject(); //get parent item
        QQmlComponent component(view.engine(),QUrl("qrc:/child.qml")); //load child item
        QQuickItem *child = qobject_cast<QQuickItem*>(component.create());
        child->setParentItem(parent);
        child->setProperty("x",parent->width()/2-child->width()/2);
        child->setProperty("y",parent->height()/2-child->height()/2);
        

        157

        1 Reply Last reply
        0
        • P Offline
          P Offline
          PhTe
          wrote on last edited by
          #4

          The second point seems to be a good workaround.

          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