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"
    }
相关推荐
sycmancia7 小时前
Qt——多线程中的信号与槽(二)
jvm·qt
Quz1 天前
QML Label 完整用法与 Text 组件选型
前端·qt
kaixin_learn_qt_ing1 天前
QPluginLoader
qt
kaixin_learn_qt_ing1 天前
Qt 提升部件(Promote)找不到头文件路径
qt
秋田君2 天前
QT_QT布局详解
开发语言·数据库·qt
辞旧 lekkk2 天前
【Qt系统相关】鼠标事件
linux·开发语言·qt·学习·计算机外设·萌新
brave_zhao2 天前
什么是Qt Designer
开发语言·qt
贺国亚3 天前
A2A协议与Agent互操作-Task生命周期
开发语言·qt
luoyayun3613 天前
Qt + FFmpeg 视频工具:视频一键压缩功能实现
qt·ffmpeg·音视频·视频压缩
blueman88883 天前
Qt5通过vcpkg中调用时,在debug模式下调试时总是调用release的plugins文件夹中的dll
c++·qt·cmake