如何将 qml 文件重新加载到 QQuickView

2022-01-19 00:00:00 qt qml c++

将 qml 文件重新加载到 QQuickView 的正确方法是什么?我正在使用 Qt Quick 2.1 并尝试编写一个简单的程序来加载 qml 文件并显示它.目前我正在通过创建一个 QQuickView 来做到这一点,当我想重新加载 qml 文件时,我正在删除旧文件并创建一个新文件.这样做的正确方法是什么?使用新的 qml 文件(或更改的 qml 文件)调用 QQuickView::setSource 对我不起作用.

what is the proper way of reloading qml file to QQuickView? I'm using Qt Quick 2.1 and trying to write a simple program that loads a qml file and displays it. Currently I'm doing it by creating a QQuickView and when i want to reload qml file i am deleting the old one and creating a new one. What is the proper way of doing this? calling QQuickView::setSource with new qml file (or changed qml file) didn't worked for me.

推荐答案

你可以使用以下方式(假设你在QQuickView的子类中):

You can use the following (assuming you are in a subclass of QQuickView):

QUrl tmp = source();
setSource(QUrl());
engine()->clearComponentCache();
setSource(tmp);

相关文章