0

I am trying to save my application settings to an INI file using QSettings; however, I keep getting this error: AttributeError: type object 'QSettings' has no attribute 'IniFormat'.

Here is my code:

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

# QSettings initialized 
readSettings = QtCore.QSettings(filename, QtCore.QSettings.IniFormat)
readSettings.setIniCodec("UTF-8")
readSettings.sync()

What am I doing wrong? Thanks in advance.

  • Emums have changed in PyQt6, you need the full name: `QtCore.QSettings.Format.IniFormat`. – musicamante Aug 30 '21 at 20:57
  • @musicamante is there a specific location where I can find the updated syntax for PyQt6? – Gillian Grace Aug 30 '21 at 21:03
  • Some information on the official site: https://www.riverbankcomputing.com/static/Docs/PyQt6/pyqt5_differences.html In the end, you just need to follow the documentation and use the enum name for its definition: `Module.Class.EnumType.Name`. The `IniFormat` is a [`QSettings::Format`](https://doc.qt.io/qt-6/qsettings.html#Format-enum) enum, so the full name is `QtCore.QSettings.Format.IniFormat`. – musicamante Aug 30 '21 at 21:09
  • @musicamante thank you. Now I am getting a `AttributeError: 'QSettings' object has no attribute 'setIniCodec'` error. I looked on this site: [link](https://doc.qt.io/qt-5/qsettings.html#setIniCodec-1) but I am still confused how to make it work for PyQt6. – Gillian Grace Aug 30 '21 at 21:37
  • Since you're using PyQt6, you must comply to the documentation about Qt6, which removed support for that (your link refers to Qt5, as the url and the header of that page report). See [Compatibility with older Qt versions](https://doc.qt.io/qt-6/qsettings.html#compatibility-with-older-qt-versions). Remember that, while Qt6 has been released 8 months ago and it's already quite stable, it's still in slightly early stage of development, many components have been temporarily disabled during the transition, and, since it's a major release, many features and functions are deprecated and removed. – musicamante Aug 30 '21 at 22:17
  • So, unless you *really* need to use Qt6 and/or are already *very* experienced with PyQt, you should probably keep using PyQt5 for some time while waiting for more mature releases, otherwise you should consider that many aspects require changes (and knowledge/experience) especially because lots of tutorials and documentation refer to Qt5, and some features might be unsupported or behave differently, and then pay anyway a lot of attention on what you're trying to use by always checking the *correct* documentation. – musicamante Aug 30 '21 at 22:21
  • @musicamante Unfortunately I have already written my program in PyQt6 so I don't plan on switching to PyQt5 (unless I run into a complete roadblock). I don't see `setIniCodec()` as one of the supporting functions so I will edit my code accordingly. Thank you for your time. – Gillian Grace Aug 30 '21 at 22:33

0 Answers0