Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop QSettings altering the order of key=value pairs in setting file?

Tags:

ini

qt5

qsettings

In my program I have a Microsoft's INI style settings/configuration file that is created, edited and stored using the convenient QSettings class but the user may manually edit this file using the program itself, or any text editor she desires like gedit or vim. The order in which key=value pairs appear is important. The problem is whenever I try to store the changes at shutdown time, the order of key=value pairs change to a random order and it seems impossible to stop QSettings from changing it. For clarification here's a sample of a configuration file:

[AlarmGroup1]
DateTimeNotInitialized=1
DateTimeStampError=2
ParametersMissingOrInconsistent=3
NotInitialized=4
FlashMemoryFatalError=5
NotIdentified=6

which changes to:

[AlarmGroup1]
ParametersMissingOrInconsistent=3
DateTimeNotInitialized=1
DateTimeStampError=2
NotInitialized=4
FlashMemoryFatalError=5
NotIdentified=6

Is it possible to avoid the change of order? Why is Qsettings behaving like this?

like image 978
moki Avatar asked Nov 12 '22 17:11

moki


1 Answers

QSettings behaves like this because the problem it was designed to solve was saving/retrieving single key/value pairs, not ordered lists of pairs.

To solve this you will need to either :

  • Remove the requirement for a specific order from you program
  • Write your own equivalent of QSettings
  • Write a "custom storage format provider" to pass into QSettings http://doc.qt.io/qt-5/qsettings.html#registerFormat
like image 73
Bigwave Avatar answered Dec 24 '22 01:12

Bigwave