如何从C ++创建Qml组件?

时间:2015-02-17 17:13:05

标签: c++ qt qml

我尝试使用QQmlComponent组件(view.engine(),QUrl :: fromLocalFile(“MyItem.qml”));然后是QOObject * object = component.create();但它让我没有准备好Qml组件。进一步尝试将statusChanged信号连接到插槽函数,但它似乎没有加载新的qml组件。

QQuickView view;
view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
QQmlComponent component(view.engine(), QUrl::fromLocalFile("MyItem.qml"));
QObject *object = component.create();
object->setParent(view.rootObject());
view.show()

2 个答案:

答案 0 :(得分:0)

如果您要从本地文件加载,请将QQmlComponent设置为同步加载,方法是在构造函数中指定它:

QQmlComponent component(view.engine(),
                        QUrl::fromLocalFile("MyItem.qml"),
                        QQmlComponent::PreferSynchronous );

答案 1 :(得分:0)

请参阅Interacting with QML Objects from C++

  

从C ++加载QML对象

     

可以使用QQmlComponent或QQuickView加载QML文档。 QQmlComponent将QML文档作为C ++对象加载,然后可以从C ++代码进行修改。 QQuickView也可以这样做,但由于QQuickView是一个QWindow派生类,加载的对象也将呈现为可视化显示; QQuickView通常用于将可显示的QML对象集成到应用程序的用户界面中。

     

...

此外,更广泛的主题Integrating QML and C++

相关问题