zl程序教程

您现在的位置是:首页 >  工具

当前栏目

QML用Qt.labs.settings实现保存用户设置

Qt 实现 设置 用户 保存 settings qml Labs
2023-09-11 14:19:24 时间

举个简单的例子:
main.cpp中设置程序信息

QGuiApplication::setApplicationName("Gallery");
QGuiApplication::setOrganizationName("QtProject");
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

在需要保存设置的qml中:

...
import Qt.labs.settings 1.0
...{
   ...
    Settings {
        id: settings
        property string input: "Default"
    }
    TextInput{
        width:200
        height: 50
        id: textInput
        text: settings.input
    }
    Button{
        anchors.top:textInput.bottom
        text:"save"
        onClicked: {
            settings.input=textInput.text
        }
    }
   ...
}

程序关闭后,再次启动时信息还会在文本框内。