QML 中的设置值保存在哪里?
Where are Settings values saved in QML?
我正在为 C++/QML 应用程序使用 QtCreator。我想保存一些应用程序设置,我使用与此 link.
中的文档非常相似的代码实现了这个目标
...
import Qt.labs.settings 1.0
Settings {
id: settingsValues
property int sliderValue: 300000
property bool checkBoxChecked: true
}
...
问题
1) 这些值保存在哪里?也许有专门为存储这些变量而生成的文件?我怎样才能找回这个文件?
2) 应用程序如何知道它必须加载这些值?
问题 1 由 documentation 回答:
The information is stored in the system registry on Windows, and in XML preferences files on macOS. On other Unix systems, in the absence of a standard, INI text files are used. See QSettings documentation for more details.
问题2:如果设置中存在,则为loaded。
相关代码是(撰写本文时的第 311 行)。 if 子句 决定应用程序是否加载该值:
if (!currentValue.isNull() && (!previousValue.isValid()
|| (currentValue.canConvert(previousValue.type()) && previousValue != currentValue))) {
property.write(q, currentValue);
qCDebug(lcSettings) << "QQmlSettings: load" << property.name() << "setting:" << currentValue << "default:" << previousValue;
}
我选择将@derM 的答案保留为已接受的答案,因为它对问题 2 很有帮助。
尽管如此,我 post 这个答案是因为在我的情况下它是问题 1 的正确答案。
我在Ubuntu 16.04上使用QtCreator,所以我在以下路径找到了配置文件:
default_Directory_Of_My_Project/.config/Unknown Organization/myProject.conf
我在 macOS 上,我确实在我的用户目录中搜索了所有可能的地方,但从未找到它。
我的代码是:
Settings {
id: settings
fileName: "settings"
property alias x: root.x
property alias y: root.y
}
终于在我的build目录下找到了(准确的说是在Projects > Build Settings > Build directory中设置的路径)
在.app/Contents/MacOS/
并且文件被命名为 fileName
属性 (所以 settings.xml 对我来说)
回顾一下,在 macOS 上:build_directory/XXX.app/Contents/MacOS/ fileName
(对我来说:XXX.app/Contents/MacOS/settings)
我正在为 C++/QML 应用程序使用 QtCreator。我想保存一些应用程序设置,我使用与此 link.
中的文档非常相似的代码实现了这个目标...
import Qt.labs.settings 1.0
Settings {
id: settingsValues
property int sliderValue: 300000
property bool checkBoxChecked: true
}
...
问题
1) 这些值保存在哪里?也许有专门为存储这些变量而生成的文件?我怎样才能找回这个文件?
2) 应用程序如何知道它必须加载这些值?
问题 1 由 documentation 回答:
The information is stored in the system registry on Windows, and in XML preferences files on macOS. On other Unix systems, in the absence of a standard, INI text files are used. See QSettings documentation for more details.
问题2:如果设置中存在,则为loaded。
相关代码是(撰写本文时的第 311 行)。 if 子句 决定应用程序是否加载该值:
if (!currentValue.isNull() && (!previousValue.isValid()
|| (currentValue.canConvert(previousValue.type()) && previousValue != currentValue))) {
property.write(q, currentValue);
qCDebug(lcSettings) << "QQmlSettings: load" << property.name() << "setting:" << currentValue << "default:" << previousValue;
}
我选择将@derM 的答案保留为已接受的答案,因为它对问题 2 很有帮助。 尽管如此,我 post 这个答案是因为在我的情况下它是问题 1 的正确答案。
我在Ubuntu 16.04上使用QtCreator,所以我在以下路径找到了配置文件:
default_Directory_Of_My_Project/.config/Unknown Organization/myProject.conf
我在 macOS 上,我确实在我的用户目录中搜索了所有可能的地方,但从未找到它。
我的代码是:
Settings {
id: settings
fileName: "settings"
property alias x: root.x
property alias y: root.y
}
终于在我的build目录下找到了(准确的说是在Projects > Build Settings > Build directory中设置的路径)
在.app/Contents/MacOS/
并且文件被命名为 fileName
属性 (所以 settings.xml 对我来说)
回顾一下,在 macOS 上:build_directory/XXX.app/Contents/MacOS/ fileName
(对我来说:XXX.app/Contents/MacOS/settings)