[英]How do I create a Qml component from C++?
I tried to use QQmlComponent component(view.engine(), QUrl::fromLocalFile("MyItem.qml"));我尝试使用 QQmlComponent 组件(view.engine(), QUrl::fromLocalFile("MyItem.qml")); and then QOObject *object = component.create();
然后 QOObject *object = component.create(); but it gives me Qml Component not ready.
但它让我 Qml 组件没有准备好。 Further tried to connect the statusChanged signal to a slot function, but it doesnt seem to load the new qml components.
进一步尝试将 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()
If you're loading from a local file, then set the QQmlComponent
to load synchronously by specifying it in the constructor:如果您从本地文件加载,则通过在构造函数中指定它来设置
QQmlComponent
以同步加载:
QQmlComponent component(view.engine(),
QUrl::fromLocalFile("MyItem.qml"),
QQmlComponent::PreferSynchronous );
See Interacting with QML Objects from C++ .请参阅从 C++ 与 QML 对象交互。
Loading QML Objects from C++
从 C++ 加载 QML 对象
A QML document can be loaded with QQmlComponent or QQuickView.
可以使用 QQmlComponent 或 QQuickView 加载 QML 文档。 QQmlComponent loads a QML document as a C++ object that can then be modified from C++ code.
QQmlComponent 将 QML 文档作为 C++ 对象加载,然后可以从 C++ 代码修改该对象。 QQuickView also does this, but as QQuickView is a QWindow-derived class, the loaded object will also be rendered into a visual display;
QQuickView 也是这样做的,但是由于 QQuickView 是 QWindow 派生类,所以加载的对象也会被渲染成可视化显示; QQuickView is generally used to integrate a displayable QML object into an application's user interface.
QQuickView 通常用于将可显示的 QML 对象集成到应用程序的用户界面中。
...
...
Also, more wide topic Integrating QML and C++ .此外,更广泛的主题集成 QML 和 C++ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.