如何从 C++ 创建 Qml 组件?
How do I create a Qml component from C++?
我尝试使用QQmlComponent组件(view.engine(), QUrl::fromLocalFile("MyItem.qml"));然后是 QOObject *object = component.create();但它给我 Qml Component not ready。进一步尝试将 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()
如果您从本地文件加载,则通过在构造函数中指定它来将 QQmlComponent
设置为同步加载:
QQmlComponent component(view.engine(),
QUrl::fromLocalFile("MyItem.qml"),
QQmlComponent::PreferSynchronous );
参见Interacting with QML Objects from C++。
Loading QML Objects from C++
A QML document can be loaded with QQmlComponent or QQuickView. QQmlComponent loads a QML document as a C++ object that can then be modified from C++ code. QQuickView also does this, but as QQuickView is a QWindow-derived class, the loaded object will also be rendered into a visual display; QQuickView is generally used to integrate a displayable QML object into an application's user interface.
...
此外,更广泛的主题 Integrating QML and C++。
我尝试使用QQmlComponent组件(view.engine(), QUrl::fromLocalFile("MyItem.qml"));然后是 QOObject *object = component.create();但它给我 Qml Component not ready。进一步尝试将 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()
如果您从本地文件加载,则通过在构造函数中指定它来将 QQmlComponent
设置为同步加载:
QQmlComponent component(view.engine(),
QUrl::fromLocalFile("MyItem.qml"),
QQmlComponent::PreferSynchronous );
参见Interacting with QML Objects from C++。
Loading QML Objects from C++
A QML document can be loaded with QQmlComponent or QQuickView. QQmlComponent loads a QML document as a C++ object that can then be modified from C++ code. QQuickView also does this, but as QQuickView is a QWindow-derived class, the loaded object will also be rendered into a visual display; QQuickView is generally used to integrate a displayable QML object into an application's user interface.
...
此外,更广泛的主题 Integrating QML and C++。