You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQt6使用QSettings如何启用IniFormat,解决无该属性的AttributeError报错

问题根因

该错误由PyQt6相对PyQt5的接口不兼容变更导致,涉及两处改动:

  1. INI格式枚举值的位置发生了迁移,PyQt5中QSettings.IniFormat在PyQt6中被收纳到QSettings.Format枚举类下,正确写法为QtCore.QSettings.Format.IniFormat
  2. PyQt6已移除setIniCodec方法,默认使用UTF-8编码处理INI文件,无需额外设置编码

修正后代码

home_dir = str(Path.home())
self.fileConfig = QtWidgets.QFileDialog.getOpenFileName(self, 'Load Configuration', home_dir)
filename =  self.fileConfig[0]

# 增加非空判断,避免用户取消选择文件时报错
if filename:
    # 修正QSettings初始化参数
    readSettings = QtCore.QSettings(filename, QtCore.QSettings.Format.IniFormat)
    readSettings.sync()
  • 额外说明:sync()方法一般不需要主动调用,QSettings会自动完成配置的持久化同步,仅在需要立即落盘的场景下主动调用即可。

内容的提问来源于stack exchange,提问作者Gillian Grace

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 18:48:04