Using QSettings with data stored in a QByteArray, or QIODevice
-
I have data in ini format stored in a
QByteArray
, and I'd like to be able to read and write to it usingQSettings
. Unfortunately, out of the boxQSettings
only seems capable of working with files specifically, and can't be used with aQByteArray
or an instance of a class that derives fromQIODevice
such asQBuffer
.So far, I've found
two waysone way of dealing with this:- Dump the data temporarily into a file, probably using
QTemporaryFile
, and read that usingQSettings
Create a custom implementation of(edit: I've just discovered thatQAbstractFileEngine
that can read from the data in the byte array as though it were a file, as suggested in this ten year old thread.QAbstractFileEngine
is no longer public in Qt 5, so it's not a viable solution for me.)
Neither of these options are very appealing. Ten years has gone by since the suggestion of using
QAbstractFileEngine
. Is there a better method available now?If this is currently not possible, consider this a feature suggestion that
QSettings
be able to work with anything derived fromQIODevice
that has the necessary capabilities. - Dump the data temporarily into a file, probably using
-
Use a QTemporaryFile and provide a patch to allow a QIODevice.
-
Writing a patch would be a massive undertaking and one I most definitely don't have time for right now. 😝
I'm still curious if there's a method available other than using QTemporaryFile.
-
@Guy-Gizmo said in Using QSettings with data stored in a QByteArray, or QIODevice:
consider this a feature suggestion
This is an user forum.
If you want to give suggestion to dev team you should file an issue to https://bugreports.qt.io -
@Christian-Ehrlicher said in Using QSettings with data stored in a QByteArray, or QIODevice:
provide a patch
Are you talking about making a format and registering it?
I was thinking something from boost would be helpful. Not sure how the format interface works though.
-
@fcarney said in Using QSettings with data stored in a QByteArray, or QIODevice:
I was thinking something from boost would be helpful. Not sure how the format interface works though.
I considered adding another format that uses a separate ini parser that can handle data in memory, but that only gets me halfway there. I need to be able to read ini formatted files already written using QSettings, which means that certain types (for example QByteArray, QStringList, etc.) have been encoded using a method specific to QSettings' ini formatter. As far as I know QSettings doesn't expose its facility for encoding and decoding those values.
-
Just a quick follow-up: I ended up using
QAbstractFileEngine
to solve this anyway, even though it's private in Qt 5.15. If it ever is removed or otherwise stops becoming a viable method, I'll update my code accordingly.That said, if anyone can suggest another viable method that doesn't rely on writing the data out to a temporary file, I'd love to hear it!