QSettings, my own format of config-file
Hello!
Has anybody worked with QSettings to make own format of config-files?
I want to store all settings in xml, but I have a trouble:
after reading a doc I understood that I should create my own QSettings::Format
I need readFunc and writeFunc functions.
From doc:
Quote:
typedef QSettings::WriteFunc
Typedef for a pointer to a function with the following signature:
bool myWriteFunc(QIODevice &device, const QSettings::SettingsMap &map);
---
typedef QSettings::SettingsMap
Typedef for QMap<QString, QVariant>.
Can anybody give me an example of QSettings::SettingsMap?
Thanks!
PS: sorry for my English, my native language is Russian ;-)
Re: QSettings, my own format of config-file
But it is just a QMap of key/value pairs. The keys are the entries in your settings whereas the QVariants are their values.
In readFunc you populate the SettingsMap from file and in writeFunc you write the map to your custom file.
Regards
QSettings with XML format
Here is my solution:
Code:
bool readXmlFile
( QIODevice
& device,
QSettings::SettingsMap& map
) {
QXmlStreamReader xmlReader( &device );
while( !xmlReader.atEnd() )
{
xmlReader.readNext();
while( xmlReader.isStartElement() )
{
if( xmlReader.name() == "SettingsMap" )
{
xmlReader.readNext();
continue;
}
if( !currentElementName.isEmpty() )
{
currentElementName += "/";
}
currentElementName += xmlReader.name().toString();
xmlReader.readNext();
}
if( xmlReader.isEndElement() )
{
continue;
}
if( xmlReader.isCharacters() && !xmlReader.isWhitespace() )
{
QString value
= xmlReader.
text().
toString();
map[ key ] = value;
currentElementName.clear();
}
}
if( xmlReader.hasError() )
{
return false;
}
return true;
}
bool writeXmlFile
( QIODevice
& device,
const QSettings::SettingsMap& map
) {
QXmlStreamWriter xmlWriter( &device );
xmlWriter.setAutoFormatting( true );
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement( "SettingsMap" );
QSettings::SettingsMap::const_iterator mi
= map.
begin();
for( mi; mi != map.end(); ++mi )
{
std::vector< std::string > groups;
StringUtils::SplitList( mi.key().toStdString().c_str(), "/", &groups );
foreach( std::string groupName, groups )
{
xmlWriter.writeStartElement( groupName.c_str() );
}
xmlWriter.writeCharacters( mi.value().toString() );
foreach( std::string groupName, groups )
{
xmlWriter.writeEndElement();
}
}
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
return true;
}
I just saw that QString has a Split method. Use that instead of my StringUtils::SplitList call.