PDA

View Full Version : QSettings write and read function



ivareske
13th February 2011, 11:26
When using ini files, does anyone know how QSettings transforms say a QStringList (or any other object) into the text that is written to the ini file? I am trying to encrypt the data before it is written. It would be great to just access the default read and write functions of qsettings, but I don`t find any info on these functions... Another possibility could be to tell QSettings to read/write using the standard functions to a QBuffer, then encrypt and then write to file. Doesn anyone know how to do this?

-Ivar

wysota
14th February 2011, 00:00
When using ini files, does anyone know how QSettings transforms say a QStringList (or any other object) into the text that is written to the ini file?
Qt is open-source. The easiest way is to look at the code responsible for this.

But since you want to do mangle the data I don't see the point in duplicating what QSettings does for ini files. Just provide your own read and write functions that save to a custom format and do the encryption as part of the process. The simplest possible read/write combination is:

bool readFile(QIODevice &device, QSettings::SettingsMap &map) {
QDataStream stream(&device);
device >> map;
return true;
}

bool writeFile(QIODevice &device, const QSettings::SettingsMap &map) {
QDataStream stream(&device);
device << map;
return true;
}

ivareske
24th February 2011, 16:04
Ok, but I still don`t understand how to convert my custom classes to QString. The map contains my custom object as a QVariant, how to I convert that to string so I can encrypt it?

wysota
24th February 2011, 16:14
I don't see your point. You can encrypt data regardless of the format it is in.

bool writeFile(QIODevice &device, const QSettings::SettingsMap &map) {
QBuffer buffer;
buffer.open(QIODevice::WriteOnly);
QDataStream stream(&buffer);
device << map;
QByteArray encrypted = encrypt(buffer.data());
device.write(encrypted);
return true;
}