gpt4 book ai didi

javascript - 在运行时动态更改 QML 主题

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:14:17 25 4
gpt4 key购买 nike

我实际上使用的是此处提供的解决方案:https://stackoverflow.com/a/25864815/2425044

我想摆脱 import "MyTheme.js"as Theme; 语句,以便在运行时动态加载特定主题(通常由用户选择)。

我目前正在做的是将我的每个 Themes.js 文件加载到 qrc 文件中:

  • redTheme.qrc 包含 Theme.js
  • blueTheme.qrc 包含 Theme.js

这些qrc文件被编译成外部二进制资源(rcc)并从二进制目录加载,使用

registerResource(const QString &rccFileName, const QString &mapRoot = QString())

到目前为止,一切正常。唯一的问题是我在 QML 文件中遇到了 import 语句:

import "qrc:/redTheme/Theme.js" as Theme

因此,尽管将blueTheme.rcc 注册为资源,但它永远不会被使用。

最佳答案

感谢其他线程,我能够让它工作。

首先,像这位用户一样创建您的主题,它继承自 AbstractStyle,从而提供更大的灵 active 。

https://stackoverflow.com/a/25866188/2425044

我们的 property 将由 JS 函数返回的值定义:

import "qrc:/graphics/componentCreation.js" as Theme

Item
{
id: homeViewItem
anchors.centerIn: parent

// Load default theme at startup
property AbstractTheme currentTheme: Theme.createThemeObject(homeViewItem, "qrc:/redTheme/redTheme.qml");

Rectangle
{
color: currentTheme.textColorStandard;
}
}

componentCreation.js

// Create themes components and load them in the apps' QML files

var component;
var sprite;

function createThemeObject(item, themePath)
{
component = Qt.createComponent(themePath);
sprite = component.createObject(item);

if (sprite === null)
console.log("componentCreation.js: error creating " + themePath + " object");
else
return sprite;
}

假设您想要在用户单击 Button 时更改主题:

Button
{
id: themeButton
text: "Change to blue theme"
onClicked:
{
// Remove content of redTheme.rcc, register blueTheme.rcc
cpp_class.changeTheme("redTheme", "blueTheme")
// blueTheme's content is now available, let's fill its content into a QML object
currentTheme = Theme.createThemeObject(homeViewItem, "qrc:/blueTheme/blueTheme.qml")
}
}

请记住,redTheme.qmlblueTheme.qml 包含在 qrc 文件中,这些文件本身会编译成 rcc 文件。

下面是changeTheme(const QString&, const QString&)的定义,注销旧主题并注册新主题:

void cpp_class::changeTheme(const QString &oldTheme, const QString &newTheme)
{
bool un = QResource::unregisterResource(QCoreApplication::applicationDirPath() + "/data/themes/" + app + "/" + oldTheme + ".rcc");
if (!un)
std::cerr << oldTheme.toStdString() << "could not be unregistered" << std::endl;
bool in = QResource::registerResource(QCoreApplication::applicationDirPath() + "/data/themes/" + app + "/" + newTheme + ".rcc");
if (!in)
std::cerr << newTheme.toStdString() << "could not be registered as an external binary resource" << std::endl;
}

其他对我有帮助的帖子:

https://wiki.qt.io/Qml_Styling

http://www.slideshare.net/BurkhardStubert/practical-qml-key-navigation (从幻灯片 34 开始)

关于javascript - 在运行时动态更改 QML 主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36335481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com