如何使用C ++创建Qml组件? [英] How do I create a Qml component from C++?

查看:57
本文介绍了如何使用C ++创建Qml组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried to use QQmlComponent component(view.engine(), QUrl::fromLocalFile("MyItem.qml")); and then QOObject *object = component.create(); but it gives me Qml Component not ready. Further tried to connect the statusChanged signal to a slot function, but it doesnt seem to load the new qml components.

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()

推荐答案

如果要从本地文件加载,请通过在构造函数中指定 QQmlComponent 以使其同步加载:

If you're loading from a local file, then set the QQmlComponent to load synchronously by specifying it in the constructor:

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

这篇关于如何使用C ++创建Qml组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆