setContextProperty 和 setProperty of object 的区别

Diiference between setContextProperty and setProperty of object

我现在很迷茫

有什么区别

QQmlApplicationEngine engine;
engine.rootContext().setContextProperty("myObject",&userData);

object->setProperty("myObject", myObject)

这是 QML 文件

ApplicationWindow {
id: applicationWindow1

Item {
    id: propertyHolder
    property MyObject myObject
}

我已经阅读了如何使用 QML 绑定,但仍未弄明白。请帮忙 谢谢

编辑:======================= 我在此处附上代码片段

ApplicationWindow {
id: applicationWindow1

Item {
    id: propertyHolder
    property MyClass myClass
}

Button {
    onClicked : 
        propertyHolder.myClass.doSomething()
}

main.cpp

QQmlApplicationEngine engine;
QQmlContext* context = engine.rootContext();

MyClass myClass;
context->setContextProperty("myClass",&myClass);
engine.load(QUrl("qrc:///mainControl.qml"));

当我点击按钮时,它给了我一个调用方法的空错误 我哪里错了?

setPropertyQObject 的成员,用于设置 QObject 的 属性 的值。 setContextPropertyQQmlContext class 的成员,用于在 qml 上下文中设置名称 属性 的值。您可以阅读有关 QQmlContext 的 Qt 文档:

Each QQmlContext contains a set of properties, distinct from its QObject properties, that allow data to be explicitly bound to a context by name. The context properties are defined and updated by calling QQmlContext::setContextProperty().