1
votes

Need: Read/Write a struct of standard Qt types from/to a human readable/editable file.

Tried: QSettings. It was fairly easy to get it working. Problem: QSettings automatically calls QSettings::sync() periodically, and at dtor. I am required to ensure that the settings file is only updated on request, and that run-time data structure is only modified on request.

Tried: QDataStream. Problem: Not human readable/editable.

Tried: Manual serialization and deserialization, using standard readline and QString::toInt() and such. I now have to support types that QSettings supports, but that QString does not have 'toFoo()' methods for. This way would be MUCH, MUCH more coding.

Is there a way to serialize and deserialize human-readable data without using QSettings? Everything about it is against requirements. The files are supposed to be named differently. Access is supposed to be controlled differently.

It would probably be enough, if there was simply a way to block sync(). We can probably work around anything else. It's "sync" that gives me the most problems.

1
You could use QSettings not as a member variable of your class, but a local variable which is created or deleted in different places of your code depending on your requirements.Tarod
What problems do you get with QTextStream ?Dmitry Sazonov
If you want human readable it's probably worthwhile doing your own serialisation with JSON. There's an example here: doc.qt.io/qt-5/qtcore-json-savegame-example.htmlNicholas Smith
@Tarod That might work. I might be able to protect file by changine file name in QSettings, and protect QSettings instance... By carefully paying attention to the event loop. If I can guarantee we won't re-enter it between calls, that might be enough. @ SaZ It doesn't deserialize automatically or for all the types QSettings does, AFAICT. @ NicholasSmith If I had to resort to manually creating the serialization format, we wouldn't use JSON. I don't think those classes actually help me. I still have to write read()/write().SaburoutaMishima
@SaburoutaMishima Yep, the idea would be to implement by yourself a sort of synchronization because as you said, you require to update the file on request.Tarod

1 Answers

0
votes

After looking at the source code for QSettings, it appears that it performs human-readable serialization-deserialization in a unique way. It does not appear that this facility exists in other parts of Qt. Also, there are numerous points where the file-system is automatically altered.

This is not possible in Qt.