[Moved] How to use QSettings to read and write from xml file?
-
Hi All,
please tell me how to save and read current QMainWindow settings to/from a xml file. -
As far as I know there is no built-in solution. But it can be done easily using "QXmlStreamReader":http://doc.qt.nokia.com/latest/qxmlstreamreader.html, "QXmlStreamWriter":http://doc.qt.nokia.com/latest/qxmlstreamwriter.html and "QSettings::registerFormat()":http://doc.qt.nokia.com/stable/qsettings.html#registerFormat.
There are even implementations on the internet "here":http://www.openshots.de/2011/03/qsettings-mit-xml-format/ and "here":http://pastebin.com/M6Lqac91. No testing. No warranty.
-
You can also look at QMainWindow's "saveState,":http://doc.qt.nokia.com/4.7-snapshot/qmainwindow.html#saveState "restoreState,":http://doc.qt.nokia.com/4.7-snapshot/qmainwindow.html#restoreState "saveGeometry,":http://doc.qt.nokia.com/4.7-snapshot/qwidget.html#saveGeometry and "restoreGeometry.":http://doc.qt.nokia.com/4.7-snapshot/qwidget.html#restoreGeometry
-
Hi Lukas,
Well the version that havePySide version: 1.0.2
PySide.QtCore version: 4.7.2
PySide.QtCore.qVersion :4.7.2
*QtCore.QSettings.registerFormat() - is missing*
please tell me what should i do?
-
When using Python you are probably hosed because there is no registerFormat (as you already figured out).
-
Hi,
So you mean there is no alternative way to save/read setting from XML?
If yes ,please let me know
please put me in the right direction. -
I have to admit that I'm no Python expert but no, I don't think there is any convenient way to use QSettings with XML files. You might re-post this question directly at "PySide":http://www.pyside.org/support/.
-
Python has its own XML parsing modules, and all of them are pretty good. As PySide generally uses native Python types (lists, dicts, Python strings, etc.) for most of the data interchanging, using a native Python parser instead of QSettings does not really affect the rest of the codes, so I think you might just ditch QSettings in this case.
If you intends to do some lite-weight parsing, the default Python installation comes with xml.etree.ElementTree, which is very easy to use.
If you are not satisfied with ElementTree's performance, you can also take a look at lxml.etree, which is considered by many as the best parser around (for Python).
-
The QSettings API basically just provides a sectioned list of key-value pairs. Why would you want to use it to interact with a possibly deeply nested hierarchical structure like XML? Even if it is possible to do so (I found out that it is not trivial), the API just isn't a very good match for it.