PDA

View Full Version : QSettings problem!



0xl33t
27th July 2009, 23:12
Hi,
saving values with qsettings works out of the box for "normal" variables (not pointers), but with pointer variables it doesn't work.

WORKING VERSION:



QString myString;
int myInt;

settings = new QSettings("test", "lol");
settings->beginGroup("lol");
myString = settings->value("myString").toString();
myInt = settings->value("myInt").toInt();
settings->endGroup();

std::cout << qPrintable(myString) << std::endl;
std::cout << myInt << std::endl;


NOT WORKING VERSION


QString *myString = new QString;
int *myInt = new int;

settings = new QSettings("test", "lol");
settings->beginGroup("lol");
*myString = settings->value("myString").toString();
*myInt = settings->value("myInt").toInt();
settings->endGroup();

std::cout << qPrintable(*myString) << std::endl;
std::cout << *myInt << std::endl;


can anyone explain me why qsettings with pointer variables doesn't work?
Or where i am wrong?

wysota
28th July 2009, 00:10
How does "not working" occur? The values are incorrect, empty or what?

mcosta
28th July 2009, 07:54
How do you write the settings?

0xl33t
28th July 2009, 07:57
i didn't rewrite the settings, because i am using the same used for the working version, so the settings are saved correctly previously...

When i read in this mode the settings, the varaibles are empty! I tried myString.isEmpty() and it gives me 'true'

wysota
28th July 2009, 08:09
Works fine for me...


#include <QSettings>
#include <QString>
#include <QtDebug>

int main(int argc, char **argv){
QSettings settings("test.ini", QSettings::IniFormat);
QString *str = new QString;
if(argc<2){
*str = "xyz";
settings.setValue("test", *str);
} else {
*str = settings.value("test").toString();
qDebug() << *str;
}
return 0;
}

0xl33t
28th July 2009, 08:15
I launched make clean and recompiled the code again, now it's working fine! O.o

The same code! Very strange!


thanks anyway!!! :D