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"
    }
相关推荐
ShineWinsu6 小时前
对于Qt:事件的解析
c++·qt·面试·笔试·gui·客户端·事件
西西弗Sisyphus16 小时前
Qt QML 程序是怎么跑起来的(1)qml-only
开发语言·c++·qt
小灰灰搞电子19 小时前
QAccel 类讲解与代码示例:Qt 3 中的键盘加速键管理
qt·计算机外设
ShineWinsu2 天前
对于Qt:重写事件步骤的解析
开发语言·c++·qt·客户端·事件·鼠标事件·步骤
敲上瘾2 天前
Qt控件通用属性:geometry、qrc资源到QSS样式表等
开发语言·c++·qt·ui
ShineWinsu3 天前
对于Qt:QFontDialog的解析
c++·qt·面试·gui·字体·客户端·qfontdialog
JosieBook3 天前
【鸿蒙】把一个 Qt 桌面视频编辑器“搬“上鸿蒙 PC:Olive 移植适配全记录
qt·音视频·harmonyos
Lucas. Wáng4 天前
Qt学习之旅
c++·qt
金色熊族4 天前
qt5.15.9编译qt-creator7.0.2源码
linux·qt
ShineWinsu5 天前
对于Qt:QMessageBox的解析
开发语言·c++·qt·面试·客户端·qmessagebox·消息对话框