有一个用于记住应用程序主题的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?
答案 0 :(得分:0)
如@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
}