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"
    }
相关推荐
疋瓞6 小时前
C++_win_QT6学习《3》_结合qt项目开发学习git仓库相关知识
c++·qt·学习
锡兰_CC8 小时前
无缝触达,卓越体验:开启openEuler世界的任意门
服务器·网络·数据库·c++·图像处理·qt·nginx
Source.Liu9 小时前
【LibreCAD】RS_Pen 类详解
qt·cad
油炸自行车10 小时前
【Qt】Qt Creator Debug模式提示“缺少 Windows CDB 调试器配套的扩展组件“”
开发语言·windows·qt
我要升天!11 小时前
QT -- 网络编程
c语言·开发语言·网络·c++·qt
墨月白11 小时前
[QT]QList 相关接口
qt
GIS阵地11 小时前
Qt实现简易仪表盘
开发语言·c++·qt·pyqt·qgis·qt5·地理信息系统
电子_咸鱼13 小时前
【QT SDK 下载安装步骤详解 + QT Creator 导航栏使用教程】
服务器·开发语言·网络·windows·vscode·qt·visual studio code
CC.GG14 小时前
【Qt】Qt初识
开发语言·qt
xun_xin66615 小时前
如何解决Qt与OpenCV编译器不匹配问题
开发语言·qt·opencv