Results 1 to 8 of 8

Thread: QSettings and new QVariant Type

  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QSettings and new QVariant Type

    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?

    Qt Code:
    1. typedef struct s_mystruct
    2. {
    3. int car;
    4. double wife;
    5. bool isActive;
    6. // QHash <int,QString> map;
    7. // QRect rect;
    8. }
    9. t_mystruct;
    10. Q_DECLARE_METATYPE(t_mystruct); // registering my struct here
    11.  
    12. int main (int argc,char *argv[])
    13. {
    14. QApplication app(argc,argv);
    15. t_mystruct st;
    16. st.car = 2;
    17. st.wife = 3.5;
    18. st.isActive = false;
    19. //st.rect = QRect(1,2,3,4);
    20.  
    21. QSettings settings("someCode.xfg",QSettings::IniFormat);
    22.  
    23. settings.setValue("NoOfCars",st.car);
    24. settings.setValue("wife",st.wife);
    25. settings.setValue("isActive",st.isActive);
    26. s.setValue(st);
    27. settings.setValue("struct",s); // nothing is saved on somCode.xfg but compiled ok
    28. /* note: Process terminated with status 1 (0 minutes, 3 seconds)
    29.   is the result when i try to run my program with
    30.   settings.setValue("struct",s) line on
    31. */
    32.  
    33. QPushButton *mainWin = new QPushButton; //just to have something going
    34. mainWin->show();
    35.  
    36. return app.exec();
    37.  
    38. }
    To copy to clipboard, switch view to plain text mode 

    baray98

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSettings and new QVariant Type

    [wiki]Using custom data types with Qt[/wiki]
    J-P Nurmi

  3. #3
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings and new QVariant Type

    i added the following inmy code

    Qt Code:
    1. typedef struct s_mystruct
    2. {
    3. int car;
    4. double wife;
    5. bool isActive;
    6. QHash <int,QString> map;
    7. // QRect rect;
    8. }
    9. t_mystruct;
    10. Q_DECLARE_METATYPE(t_mystruct);
    11. qRegisterMetaTypeStreamOperators<t_mystruct>("Player");
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSettings and new QVariant Type

    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.
    J-P Nurmi

  5. #5
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings and new QVariant Type

    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.

    Qt Code:
    1. typedef struct s_mystruct
    2. {
    3. int car;
    4. double wife;
    5. bool isActive;
    6. // QHash <int,QString> map;
    7. // QRect rect;
    8. }
    9. t_mystruct;
    10.  
    11. Q_DECLARE_METATYPE(t_mystruct);
    12. inline QDataStream& operator<<(QDataStream& out, const t_mystruct& st)
    13. {
    14. out << st.car;
    15. out << st.wife;
    16. out << st.isActive;
    17. return out;
    18. }
    19. inline QDataStream& operator>>(QDataStream& in, t_mystruct& st)
    20. {
    21. in >> st.car;
    22. in >> st.wife;
    23. in >> st.isActive;
    24. return in;
    25. }
    26.  
    27.  
    28.  
    29.  
    30. int main (int argc,char *argv[])
    31. {
    32. QApplication app(argc,argv);
    33. t_mystruct st;
    34. st.car = 2;
    35. st.wife = 3.5;
    36. st.isActive = false;
    37. //st.rect = QRect(1,2,3,4);
    38.  
    39. QSettings settings("someone.xfg",QSettings::IniFormat);
    40.  
    41. settings.setValue("NoOfCars",st.car);
    42. settings.setValue("wife",st.wife);
    43. settings.setValue("isActive",st.isActive);
    44.  
    45. qRegisterMetaTypeStreamOperators<t_mystruct>("MyStruct");
    46. settings.setValue("struct",st);//<-- i got no matching call for setValue (const char[7],t_mystruct&)
    47.  
    48. .... some showing of some widget happen here just for test
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Nov 2007
    Posts
    89
    Thanked 21 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings and new QVariant Type

    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:
    Qt Code:
    1. QDataStream & operator>>(QDataStream & istream, CustomDataType & cdt)
    2. {
    3. istream >> cdt.boolValue;
    4. istream >> cdt.qstringValue;
    5. istream >> cdt.qpointValue;
    6.  
    7. return istream;
    8. }
    9.  
    10. QDataStream & operator<<(QDataStream & ostream, CustomDataType & cdt)
    11. {
    12. ostream << cdt.boolValue;
    13. ostream << cdt.qstringValue;
    14. ostream << cdt.qpointValue;
    15.  
    16. return ostream;
    17. }
    18.  
    19. ...
    20.  
    21. // Operators are used this way:
    22. CustomDataType cdt;
    23. QDataStream stream(...);
    24.  
    25. stream << cdt;
    26. stream >> cdt;
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to bender86 for this useful post:

    baray98 (11th February 2008)

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSettings and new QVariant Type

    This is also covered in the same article. You must use QVariant::fromValue() unless you provide an operator for QVariant.
    J-P Nurmi

  9. The following user says thank you to jpn for this useful post:

    baray98 (11th February 2008)

  10. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSettings and new QVariant Type

    Quote Originally Posted by baray98 View Post
    Qt Code:
    1. qRegisterMetaTypeStreamOperators<t_mystruct>("MyStruct");
    To copy to clipboard, switch view to plain text mode 
    Furthermore, the type name must match. So it should be "t_mystruct".
    J-P Nurmi

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.