Qt 6 QML Settings location 不创建指定路径文件

在 Qt QML 项目中可以使用Qt QSettings QML 版 Settings 方便数据持久化,具体使用可以参考Qt 文档,这里主要简单记录一下从Qt 5 升级到 Qt 6 后,没有创建指定的文件。在Qt 5中是使用 fileName 属性来指定文件路径,如下,能在 app 文件夹下面自动创建 qt_ui_config 文件

javascript 复制代码
// Qt 5
import Qt.labs.settings 1.0
Settings {
        id: myAppSettings
        fileName: applicationDir + "/qt_ui_config" 
        property bool isEdit: false
        property int menuIndex: 0
        property int menuPreIndex: 0
        category: "rightMenu"
    }

到 Qt 6 以后, 弃用了 fileName 属性,改用 location 属性,但有个坑,如果只是简单修改属性名,会发现指定的目录下找不到文件,但是能正常使用,这是因为 Qt 6 的 location 属性后面的路径中要添加前缀: "file:"

javascript 复制代码
// Qt 6
import QtCore
Settings {
        id: myAppSettings
        // location: applicationDir + "/qt_ui_config"  // failed 
        location: "file:" + applicationDir + "/qt_ui_config" // ok
        property bool isEdit: false
        property int menuIndex: 0
        property int menuPreIndex: 0
        category: "rightMenu"
    }
相关推荐
Littlehero_12113 小时前
QT自定义控件之内嵌报表(4)(源码开源)
开发语言·qt
lqj_本人17 小时前
鸿蒙跨端实战_QT后台切换导致OpenGLES上下文丢失:黑屏自救指南
qt·华为·harmonyos
小小龙学IT19 小时前
第七节 C++ 与 QML 交互
开发语言·qt
qq_4017004119 小时前
TCP 粘包问题深度解析:嵌入式网络开发的常见坑与解决方案
服务器·网络·qt·网络协议·tcp/ip
ShineWinsu20 小时前
对于TRAE中配置Qt的解析
开发语言·c++·ide·vscode·qt·ai·trae
小小龙学IT20 小时前
第一节 QML 背景介绍
c++·qt·js
此陆昭昭21 小时前
qt,使用qml写一个简单的界面
c++·qt
丰锋ff21 小时前
基于 Qt 的智能门禁系统(三)
开发语言·qt
wind100321 天前
Qt5Widgets.dll 缺失报错|游戏软件无法启动完整修复教程
开发语言·qt·游戏·电脑
丰锋ff1 天前
基于 Qt 的智能门禁系统(二)
开发语言·qt