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 boxQSettingsonly seems capable of working with files specifically, and can't be used with aQByteArrayor an instance of a class that derives fromQIODevicesuch 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 thatQAbstractFileEnginethat can read from the data in the byte array as though it were a file, as suggested in this ten year old thread.QAbstractFileEngineis 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
QSettingsbe able to work with anything derived fromQIODevicethat has the necessary capabilities. - Dump the data temporarily into a file, probably using
-
Use a QTemporaryFile and provide a patch to allow a QIODevice.
-
Use a QTemporaryFile and provide a patch to allow a 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 boxQSettingsonly seems capable of working with files specifically, and can't be used with aQByteArrayor an instance of a class that derives fromQIODevicesuch 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 thatQAbstractFileEnginethat can read from the data in the byte array as though it were a file, as suggested in this ten year old thread.QAbstractFileEngineis 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
QSettingsbe able to work with anything derived fromQIODevicethat has the necessary capabilities.@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 - Dump the data temporarily into a file, probably using
-
Use a QTemporaryFile and provide a patch to allow a QIODevice.
@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.
-
@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
QAbstractFileEngineto 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!