qt - 设置应用程序以记住主题:深色与浅色
问题描述
有这个 QML 设置来记住应用程序主题:
import QtQuick.Controls 2.0 as QQC2
QQC2.ApplicationWindow {
id: standaloneWindow // ID is required to be able to get properties
Material.theme: Material.Dark // Can be either Dark or Light
Component.onCompleted: {
// On launch, read theme from settings file
standaloneWindow.Material.theme = appSettings.materialTheme
}
Component.onDestruction:{
// On close, write theme to settings file
appSettings.materialTheme = standaloneWindow.Material.theme
}
Settings {
id: appSettings
category: "Theme"
property int materialTheme // Store theme as "int" type in settings file
}
}
问题
在第一次启动时(例如删除设置文件时),主题无法以Dark
. 在第一次启动时,应用程序总是以Light
主题开头,无论如何!
原因
当没有设置文件时,appSettings.materialTheme
成为0
默认int
类型。因此,0
等价于Material.Dark
枚举。这就是为什么在没有设置文件时应用程序总是以暗模式启动的原因。
问题
即使没有设置文件,如何使应用程序以轻模式启动?
到目前为止尝试过
我尝试使用alias
而不是int
,但standaloneWindow
没有要绑定的属性Material.theme
:
Settings {
// ...
property alias materialTheme: standaloneWindow.???
}
Any suggestion?
解决方案
正如@Mitch 所评论的,问题得到了解决:
Settings {
id: appSettings
category: "Theme"
// Set dark theme to be default for the very first launch (when settings file is NOT available)
property int materialTheme: Material.Dark
}
推荐阅读
- javascript - 找不到 SAPUI5 格式化程序函数
- javascript - nodejs在对象标题中使用局部变量
- database - Postgres 错误:块 xxx 中的页面标题无效,但在使用“排序方式”运行时很好
- python - 从多个字典中检索数据
- javascript - 会话存储购物车
- tensorflow - 不平衡数据集上的一类文本分类
- tensorflow - ssdlite_mobilenet_v2_coco.config 中的 min_depth 有什么作用?
- java - 如果我使用 Java 8 库编写应用程序并尝试使用 Java 6 编译器对其进行编译,构建会/应该失败吗?
- openmdao - 我无法记录/访问衍生品 2
- firebase - 如何从 Google Firebase 上的 HTML 模板生成 PDF 文档?