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"
    }
相关推荐
mengzhi啊7 分钟前
SerialPortItem对串口再封装一层,省去一些常用的步骤,不用每次都去写类
qt
用户83344239661412 小时前
从批量操作到任务中心,再到现场工作台:DeviceForge v2.10—v2.12 完整演进
qt
宛 禾14 小时前
Qt 控件入门:QLabel 从拖拽到对象树,一篇讲透
c++·qt·学习
打工仔折腾 AI19 小时前
Qt网络编程实战:UDP、TCP到HTTP的落地经验与选型
网络·人工智能·后端·python·qt·udp·ai agent 实战
Lhan.zzZ19 小时前
Linux + Qt/QML:解决 QtQuick.VirtualKeyboard.Plugins 模块加载失败与环境变量配置
linux·数据库·qt
誰能久伴不乏21 小时前
QT工业上位机一次「指令静默丢失」的排查
qt·docker·github
扶尔魔ocy2 天前
【qt openGL入门笔记】OSG实现飞机模型在网格图上飞行并进行态势标绘(四)
qt·opengl·osg
橙橙笔记2 天前
QT安卓软件的制作
android·git·qt·软件
芦芭荞2 天前
QT使用xml模版生成word文档
xml·qt·word
mengzhi啊2 天前
qt把结构体数据存入json文件
qt·json