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"
    }
相关推荐
我真的不会C1 小时前
Qt中的多种输出方式,信号与槽的基本使用
开发语言·qt
老歌老听老掉牙4 小时前
C++使用Qt Charts可视化大规模点集
c++·qt·信息可视化·点集
明月醉窗台8 小时前
Qt 入门 3 之对话框 QDialog(1)
c语言·开发语言·c++·qt
永不停转9 小时前
继承 QPaintEngine 利用 QSvgRenderer 从SVG 图片中提取路径(QPainterPath)的方法
c++·qt
@hdd10 小时前
Qt音频采集:QAudioInput详解与示例
qt·音频采集·qaudoioinput
洛克希德马丁13 小时前
Qt饼状图在图例上追踪鼠标落点
开发语言·qt·计算机外设
zhangzhangkeji14 小时前
QT6(12)3.3.1 Qt元对象系统概述:QObject 类与 QMetaObject 类,类型转换 qobject_cast<T>()。
qt
m0_5557629014 小时前
QtConcurrent
c++·qt
去往火星20 小时前
Visual Studio 2022 QT5.14.2 新建项目无法打开QT的ui文件,出现闪退情况
ide·qt·visual studio
梦回阑珊1 天前
《QT从基础到进阶·七十四》Qt+C++开发一个python编译器,能够编写,运行python程序改进版
c++·python·qt