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"
    }
相关推荐
小鱼仙官13 小时前
Qt 主界面弹出分界面
开发语言·qt
誰能久伴不乏17 小时前
用 UDP 做一条“可靠“的控制通道
c++·qt·网络协议·udp
Bczheng121 小时前
VS2022 安装MFC和QT
c++·qt·mfc
会编程的小孩1 天前
8.19号函数
qt
蓝祁子yoky2 天前
Qt学习日记 Day15:网络编程(二)- UDP基础
网络·qt·学习
读书札记20222 天前
Qt UTF-8和UTF-8(BOM)
开发语言·qt
ShineWinsu2 天前
对于Qt:QProgressBar常见属性的解析
开发语言·c++·qt·进度条·gui·定时器·qprogressbar
ShineWinsu2 天前
对于Qt:QLCDNumber常见属性以及QTimer的解析
服务器·数据库·c++·qt·gui·qtimer·qlcdnumber
听风吹等浪起2 天前
第26章:ViT+InceptionDW+FocalLoss改进|CT骨折影像二分类完整Pytorch实现(含PyQt5推理GUI)
pytorch·qt·分类
ShineWinsu2 天前
对于Qt:QRadioButton常见属性以及QButtonGroup的解析
数据库·c++·qt·microsoft·gui·qradiobutton·qbuttongroup