PDA

View Full Version : QSettings and new QVariant Type



baray98
11th February 2008, 08:34
I tried to register a struct in QVariant see code below, I was trying to test if i could save it in QSetting::setValue(key,QVariant&) and found that it won't work, can someone point out my mistake ? what did i do wrong?




typedef struct s_mystruct
{
int car;
double wife;
bool isActive;
// QHash <int,QString> map;
// QRect rect;
}
t_mystruct;
Q_DECLARE_METATYPE(t_mystruct); // registering my struct here

int main (int argc,char *argv[])
{
QApplication app(argc,argv);
t_mystruct st;
st.car = 2;
st.wife = 3.5;
st.isActive = false;
//st.rect = QRect(1,2,3,4);

QSettings settings("someCode.xfg",QSettings::IniFormat);

settings.setValue("NoOfCars",st.car);
settings.setValue("wife",st.wife);
settings.setValue("isActive",st.isActive);
QVariant s;
s.setValue(st);
settings.setValue("struct",s); // nothing is saved on somCode.xfg but compiled ok
/* note: Process terminated with status 1 (0 minutes, 3 seconds)
is the result when i try to run my program with
settings.setValue("struct",s) line on
*/

QPushButton *mainWin = new QPushButton; //just to have something going
mainWin->show();

return app.exec();

}


baray98

jpn
11th February 2008, 08:45
Using custom data types with Qt

baray98
11th February 2008, 09:46
i added the following inmy code



typedef struct s_mystruct
{
int car;
double wife;
bool isActive;
QHash <int,QString> map;
// QRect rect;
}
t_mystruct;
Q_DECLARE_METATYPE(t_mystruct);
qRegisterMetaTypeStreamOperators<t_mystruct>("Player");


and i got compile error message

I did not understand the following in the doc that you pointed me out


To be able to use custom data types with QSettings, they must be first made known to Qt's meta system, just like with QVariant, and one must provide corresponding QDataStream operators. In addition to that, one must register the stream operators:




can you please explain a little bit

baray98

jpn
11th February 2008, 10:04
qRegisterMetaTypeStreamOperators<T>() is a function. Don't place it to global namespace but call it somewhere in your code.

Please read the article from the beginning. An example of data stream operators is just above.

baray98
11th February 2008, 11:09
thank you ... i got the operator part but still my Qsettings won't accept my struct my code below, please review it and thank you.



typedef struct s_mystruct
{
int car;
double wife;
bool isActive;
// QHash <int,QString> map;
// QRect rect;
}
t_mystruct;

Q_DECLARE_METATYPE(t_mystruct);
inline QDataStream& operator<<(QDataStream& out, const t_mystruct& st)
{
out << st.car;
out << st.wife;
out << st.isActive;
return out;
}
inline QDataStream& operator>>(QDataStream& in, t_mystruct& st)
{
in >> st.car;
in >> st.wife;
in >> st.isActive;
return in;
}




int main (int argc,char *argv[])
{
QApplication app(argc,argv);
t_mystruct st;
st.car = 2;
st.wife = 3.5;
st.isActive = false;
//st.rect = QRect(1,2,3,4);

QSettings settings("someone.xfg",QSettings::IniFormat);

settings.setValue("NoOfCars",st.car);
settings.setValue("wife",st.wife);
settings.setValue("isActive",st.isActive);

qRegisterMetaTypeStreamOperators<t_mystruct>("MyStruct");
settings.setValue("struct",st);//<-- i got no matching call for setValue (const char[7],t_mystruct&)

.... some showing of some widget happen here just for test

bender86
11th February 2008, 11:13
To be able to use custom data types with QSettings, they must be first made known to Qt's meta system, just like with QVariant, and one must provide corresponding QDataStream operators. In addition to that, one must register the stream operators:
You have to define operators to insert into and get from QDataStream your class.
Here an example:

QDataStream & operator>>(QDataStream & istream, CustomDataType & cdt)
{
istream >> cdt.boolValue;
istream >> cdt.qstringValue;
istream >> cdt.qpointValue;

return istream;
}

QDataStream & operator<<(QDataStream & ostream, CustomDataType & cdt)
{
ostream << cdt.boolValue;
ostream << cdt.qstringValue;
ostream << cdt.qpointValue;

return ostream;
}

...

// Operators are used this way:
CustomDataType cdt;
QDataStream stream(...);

stream << cdt;
stream >> cdt;

jpn
11th February 2008, 11:14
This is also covered in the same article. You must use QVariant::fromValue() unless you provide an operator for QVariant.

jpn
11th February 2008, 12:40
qRegisterMetaTypeStreamOperators<t_mystruct>("MyStruct");


Furthermore, the type name must match. So it should be "t_mystruct".