Qt World Summit: Register Today!

[SOLVED] Getting anchors from C++

  • I have class which derives from QDeclarativeItem. In its geometryChanged() method I want to layout the children. I'd like to respect the anchors which are assigned to the children - how can I get the anchors property of a QDeclarativeItem?

    If I call QDelarativeItem::property("anchors") I get a QVariant, which contains a QDeclarativeAnchors*. I can't figure out how to pull a QObject* out of a QVariant. I've tried qVariantValue<QObject*>() but that returns null. QDeclarativeAnchors is a private class so I can't cast to that.

  • I haven't played with QML (yet), but having a look at the doc, couldn't you get something out of
    "QDeclarativeProperty":http://doc.qt.nokia.com/4.7/qdeclarativeproperty.html ?

    Something like
    QDeclarativeProperty p(yourObject,"anchors");
    QObject *pObj = p.object()

    No clue if it will work, but from the doc, it's worth a try.

  • Thanks. This works
    double l = QDeclarativeProperty(child, "anchors.leftMargin").read().toDouble();

Log in to reply