QML加载程序未显示对.qml文件的更改 [英] QML Loader not shows changes on .qml file
问题描述
我有 main.qml
和 dynamic.qml
文件要加载
,使用 main.qml
上的dynamic.qml Loader {}
。
dynamic.qml
文件的内容是动态的,另一个程序可能会更改其内容并将其覆盖。
因此,我编写了一些C ++代码来检测文件上的更改并触发Signal。
我的问题是我不知道如何强制Loader重新加载文件。
I have main.qml
and dynamic.qml
files that i want to load dynamic.qml
on main.qml
using Loader {}
.
Content of dynamic.qml
file is dynamic and another program may change its content and overwrite it.
So i wrote some C++ code for detecting changes on file and fires Signal.
My problem is that I don't know how can i force Loader to reload file.
这是我目前的工作:
MainController {
id: mainController
onInstallationHelpChanged: {
helpLoader.source = "";
helpLoader.source = "../dynamic.qml";
}
}
Loader {
id: helpLoader
anchors.fill: parent
anchors.margins: 60
source: "../dynamic.qml"
}
我认为QML Engine缓存 dynamic.qml
文件。因此,每当我要重新加载Loader时,它都会显示旧内容。有建议吗?
I think that QML Engine caches dynamic.qml
file. So whenever I want to reload Loader, it shows old content. Any suggestion?
推荐答案
您需要在QQmlEngine上调用 trimComponentCache()
之后,您已将Loaders source
属性设置为空字符串。换句话说:
You need to call trimComponentCache()
on QQmlEngine after you have set the Loaders source
property to an empty string. In other words:
helpLoader.source = "";
// call trimComponentCache() here!!!
helpLoader.source = "../dynamic.qml";
为此,您需要将一些C ++对象暴露给具有引用的QML到您的QQmlEngine中(在Qt和StackOverflow上有很多示例可以帮助您解决此问题。)
In order to do that, you'll need to expose some C++ object to QML which has a reference to your QQmlEngine (lots of examples in Qt and on StackOverflow to help with that).
trimComponentCache告诉QML忘记了它当前不使用的所有组件,并且只是做什么
trimComponentCache tells QML to forget about all the components it's not current using and does just what you want.
更新-详细解释:
对于例如,在某个地方定义了一个类,该类使用指向QQmlEngine的指针并公开了trimComponentCache方法:
For example, somewhere you define a class that takes a pointer to your QQmlEngine and exposes the trimComponentCache method:
class ComponentCacheManager : public QObject {
Q_OBJECT
public:
ComponentCacheManager(QQmlEngine *engine) : engine(engine) { }
Q_INVOKABLE void trim() { engine->trimComponentCache(); }
private:
QQmlEngine *engine;
};
然后,当您创建QQuickView时,将以上内容之一绑定为上下文属性:
Then when you create your QQuickView, bind one of the above as a context property:
QQuickView *view = new QQuickView(...);
...
view->rootContext()->setContextProperty(QStringLiteral("componentCache", new ComponentCacheManager(view->engine());
然后在您的QML中,您可以执行以下操作:
Then in your QML you can do something like:
helpLoader.source = "";
componentCache.trim();
helpLoader.source = "../dynamic.qml";
这篇关于QML加载程序未显示对.qml文件的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!