在运行时动态更改QML主题
Dynamically change QML theme at runtime
我实际上使用的是这里提供的解决方案: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
,允许更大的灵活性。
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.qml和blueTheme.qml 以下是 其他帮助过我的线程: https://wiki.qt.io/Qml_Styling http://www.slideshare.net/BurkhardStubert/practical-qml-key-navigation(从幻灯片34开始)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;
}
- 正在将数据主题添加到所有项目
- 正在检测导航到<a name=“;最新主题”></a>
- 菜单栏class=活动引导程序主题无法正常工作
- .load ajax无法正常工作whitin wordpress主题
- 如何在不使用Page.Theme属性的情况下设置页面样式和主题
- 对象获胜'如果qml中的其他地方定义了数字动画属性,则t设置动画
- 使用主题运行时portlet liferay时出现Javascript错误
- 在websql成功调用中Jquery移动主题打开按钮
- 无法加载Ace.js编辑器模式和主题(命名空间项目&AMD require.js&grunt
- 在AngularJS中使用多个主题的最佳方式是什么
- 将prettyPhoto集成到Wordpress主题中
- mqtt发布和订阅同一主题的最佳实践
- 将Jquery与wordpress主题一起使用
- 包含数组的对象的QML ListModel追加中断
- 获取gmail-api-javascript中的主题行
- WordPress下拉菜单在我的主题中不起作用
- 响应式子主题和javascript
- 带有jQuery ui主题的自动样式复选框
- 试图让我的引导程序navabar背景淡出滚动在我的wordpress主题
- 在运行时动态更改QML主题