Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QSettings how to save QMap<QString,int> into configuration file

Tags:

c++

qt

After reading Save QList<int> to QSettings, I'm tring to do the same with QMap<QString,int>. I would like the configuration file to look like this:

1111=1  
2222=3  
4444=0  

But I'm getting a compilation error:

Q_DECLARE_METATYPE(QMap<QString,int>)

Warning C4002: too many actual parameters for macro 'Q_DECLARE_METATYPE'
ConfigSettings.h(5) : error C2976: 'QMap' : too few template arguments
like image 729
user63898 Avatar asked Jul 31 '11 08:07

user63898


1 Answers

The error message you're getting is caused by the fact that the preprocessor doesn't know about templates. So it's parsing that macro call is if it had two arguments - QMap<QString and int>, which makes no sense.

To save the data as you want it, you're better of serializing it yourself to your QSettings. Something like this for writing:

settings.beginGroup("Whatever");
QMap<QString, int>::const_iterator i = map.constBegin();
while (i != map.constEnd()) {
     settings.setValue(i.key(), i.value());
     ++i;
 }
settings.endGroup();

To read the settings, use the same approach with the help of the childKeys() function.

settings.beginGroup("Whatever");
QStringList keys = settings.childKeys();
foreach (QString key, keys) {
     map[key] = settings.value(key).toInt();
}
settings.endGroup();
like image 136
Mat Avatar answered Sep 22 '22 19:09

Mat