Is it possible to access QML anchors from C++?

Lud*_*cka 3 qml qt5 qtquick2

When creating QML QQuickItems from c++ code, is it possible to access/modify anchors?

All anchors properties are declared as Q_PRIVATE_PROPERTY in QQuickItem and I don't see any method which could help.

或者我必须anchors通过使用信号/事件来自己实现吗QLayout

我需要的是例如:

Rectangle {
    // ...
    anchors.bottom: parent.bottom
    anchors.bottomMargin: 0
    anchors.top: parent.top
    anchors.topMargin: 0
    anchors.right: parent.right
    anchors.rightMargin: -10
    // ...
}   
Run Code Online (Sandbox Code Playgroud)

Emm*_*lee 5

与之前的答案所表明的不同,它既是可能的,又具有有效的用例。所有 Qt 对象,甚至是私有对象,都具有内省 API。您可以从 QML 引擎使用这些 API。它们也可以在 C++ 中使用:

qvariant_cast<QObject*>(
    item->property("anchors")
)->setProperty("top", otherItem->property("bottom"));
Run Code Online (Sandbox Code Playgroud)

请注意,由于涉及一些线程,因此存在竞争条件。

QML 仅支持模型列表。显示树或表等其他结构要么实现得很糟糕(QtQuickControls 1.x),要么效率低得令人难以置信(递归重复器)。对于某些数据结构,C++也是正确实现延迟加载的唯一方法。QMLLoader有很长的路要走,但还不是全部。