QSettings::registerFormat change from QMap to QHash
-
Hi
I am using the QSettings class to save XML content, it looks like this
bool readSettingsXml(QIODevice &device, QMap<QString, QVariant> &map); bool writeSettingsXml(QIODevice &device, const QMap<QString, QVariant> &map); static const QSettings::Format xmlFormat = QSettings::registerFormat("xml", &readSettingsXml, &writeSettingsXml);
At the moment QMap is used, but QMap sorts the entries and therefore everything is out of order. I could use QHash instead, so I tried changing to QHash but then the code wouldn't compile since the readSettingsXml & writeSettingsXml functions weren't correct. Is there anyway to use a QHash to store the settings OR is there anyway to create a QMap that doesn't result in sorted keys?
Thanks
Dave
-
One Idea I have is to prefix the key with a number like
mymap["1_mykey"] = "hi"; mymap["2_mykey"] = "there";
then I would have to strip out the number and _ prior to writing the XML tag. A bit drastic, anyone with anything better?
-
@Dave-Watts
Since this is the nature of QMap per definition (for a reason) the workaround you've posted is the only possibility. But keep in mind not to simply sort the strings, but extract the numbers first and then do the sorting upon the integer values. Or you will result in "1", "11", "2", ... for example.Btw what makes you think that QHash keeps the order?
-
@raven-worx said in QSettings::registerFormat change from QMap to QHash:
QHash
"When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered."
http://doc.qt.io/qt-5/qhash.html
Maybe I will just use letters instead of numbers
A_dataXYZ
B_dataABC -
@Dave-Watts said in QSettings::registerFormat change from QMap to QHash:
"When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered."
yes, and how does this match up with the following you said?! o.O
@Dave-Watts said in QSettings::registerFormat change from QMap to QHash:
At the moment QMap is used, but QMap sorts the entries and therefore everything is out of order. I could use QHash instead
-
QMap sorts your items, i.e. it mixes up the original order, which could be unsorted
QHash does not sort them (ie. "arbitarily ordered") though why QT choose these words who knows, its not very clear!
-
@Dave-Watts
I don't think that "arbitrarily ordered" means that keys will stay in order of addition.
I don't understand why you want the keys to be sorted in some specific order. QSettings, QMaps and XML are all based on key-value principle and don't depend on any order.
But, if you still want to order keys somehow, I guess that this have to be done in the functions readSettingsXml() and writeSettingsXml(). Perhaps with using of groups. -
@Dave-Watts said in QSettings::registerFormat change from QMap to QHash:
QHash does not sort them (ie. "arbitarily ordered") though why QT choose these words who knows, its not very clear!
It's quite clear, they are arbitrarily ordered.
QHash
is an unordered container, you are not to rely on specific order of appearance neither of keys, nor of values.@Stoyan said in QSettings::registerFormat change from QMap to QHash:
I don't think that "arbitrarily ordered" means that keys will stay in order of addition.
They won't, not in the general case. The order of keys is implementation specific - what variation of the hash table was chosen (
I think Qt uses skip-list).PS. Nope, it uses a regular linked list.