设置值在QML中保存在哪里?

时间:2018-01-04 16:36:41

标签: c++ qt qml settings

我正在将QtCreator用于C ++ / QML应用程序。我想保存一些应用程序设置,并且使用与此MouseOver上的文档非常相似的代码实现了此目标。

...
import Qt.labs.settings 1.0

Settings {
    id: settingsValues
    property int sliderValue: 300000
    property bool checkBoxChecked: true
}
...

问题

1)这些值保存在哪里?也许有一个文件生成明确存储这些变量?如何检索此文件?

2)应用程序如何知道它必须加载这些值?

3 个答案:

答案 0 :(得分:2)

问题1由documentation回答:

  

信息存储在Windows上的系统注册表中,以及macOS上的XML首选项文件中。在其他Unix系统上,如果没有标准,则使用INI文本文件。有关详细信息,请参阅QSettings文档。

问题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;
}

答案 1 :(得分:0)

我选择将@derM的答案保留为已接受的答案,因为它对问题2非常有帮助。 不过我发布这个答案是因为在我的情况下这是问题1的正确答案。

我在Ubuntu 16.04上使用QtCreator,所以我在以下路径找到了配置文件:

  

default_Directory_Of_My_Project / .config / Unknown Organization / myProject.conf

答案 2 :(得分:0)

我使用的是 macOS,我确实在我的用户目录中搜索了所有可能的地方,但从未找到。

我的代码是:

Settings {
        id: settings
        fileName: "settings"
        property alias x: root.x
        property alias y: root.y
}

最后我在我的构建目录中找到了它(准确地说,在项目>构建设置>构建目录中设置的路径) 在 .app/Contents/MacOS/ 并且该文件被命名为 fileName 属性(所以对我来说是 settings.xml

回顾一下,在 macOS 上:build_directory/XXX.app/Contents/MacOS/fileName

(对我来说:XXX.app/Contents/MacOS/settings