Load QQuickView from QByteArray

Load QQuickView from QByteArray

本文关键字:QByteArray from QQuickView Load      更新时间:2023-10-16

有没有办法用字符串数据加载QQuickView?据我所知,只有void QQuickView::setSource(const QUrl & url).

我可以用字符串数据加载QQmlComponent,但现在如何将此组件分配给QQuickView

您可以在创建QQmlComponent时传递QQuickView引擎:

QQmlComponent component(view->engine(), QUrl("path/to/file.qml"));

我最近遇到了这个问题,并使用这样的QQuickWindow::contentItem()解决了这个问题。

...
int main(int argc, char ** argv)
{
    QGuiApplication app(argc, argv);
    QQuickView view;
    QQmlComponent com(view.engine());  
    com.setData(QByteArrayLiteral("... QML source ..."), QUrl())
    QQuickItem *root = qobject_cast<QQuickItem *>(com.create());
    root->setParentItem(view.contentItem());
    view.show();
    return app.exec();
}