QML Loader 不显示 .qml 文件的更改 [英] QML Loader not shows changes on .qml file

查看:15
本文介绍了QML Loader 不显示 .qml 文件的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 main.qmldynamic.qml 文件,我想在 main.qml<上加载 dynamic.qml/code> 使用 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 引擎缓存 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";

为了做到这一点,您需要向 QML 公开一些 C++ 对象,该对象引用您的 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 Loader 不显示 .qml 文件的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆