Results 1 to 12 of 12

Thread: How to use Q_DECLARE_METATYPE correctly with a custom class?

  1. #1
    Join Date
    Nov 2009
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default How to use Q_DECLARE_METATYPE correctly with a custom class?

    Background:
    Ok, Ive started learning C++ / QT recently, but previous knowledge of Java and many other languages. I worked over the address book tutorial fairly easily, but wanted to adapt/expand it to make sure I got everything ok.
    I did this by making a class (addressdata) to store various forms of address data rather then just name/address, then adapting the code to use this class in a QList rather then the QMap.
    This worked fine, adjustments to the searching/finding functions were also fairly easy to do.

    Problem:
    However, I now need to learn how to save this data out to a file.
    QDataStream wont take the QList< addressdata> as an input.

    I found this page;
    http://wiki.qtcentre.org/index.php?t..._types_with_Qt

    Which seems to detail what I have to do...correct?

    Only at the very first step of putting a "Q_DECLARE_METATYPE(Player);" at the end of my class code, I start getting a ;

    (path)/../src/corelib/kernel/qmetatype.h:126: error: no matching function for call to `addressdata::addressdata()'
    Any ideas how I'm supposed to implement this correctly?

    My class code is;

    Qt Code:
    1. #ifndef ADDRESSDATA_H
    2. #define ADDRESSDATA_H
    3.  
    4. #include <QWidget>
    5. #include <QMap>
    6.  
    7.  
    8. class addressdata
    9. {
    10.  
    11.  
    12. public:
    13. addressdata(QString);
    14. void set(QString,int,int,int); // function inputting the data
    15. int getX();
    16. int getY();
    17. int getZ();
    18. QString getAddress();
    19. QString getName();
    20. QString getXasText();
    21. QString getYasText();
    22. QString getZasText();
    23.  
    24. void setAddress(QString);
    25. void setName(QString);
    26. void setX(int);
    27. void setY(int);
    28. void setZ(int);
    29.  
    30. ~ addressdata();
    31.  
    32. private:
    33. int Xpos;
    34. int Ypos;
    35. int Zpos;
    36. QString address;
    37. QString name;
    38.  
    39. };
    40.  
    41. Q_DECLARE_METATYPE(addressdata);
    42.  
    43. #endif // ADDRESSDATA_H
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance for any help you can give me,
    Cheers

  2. #2
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use Q_DECLARE_METATYPE correctly with a custom class?

    You are missing a default constructor in your class implementatnion.
    You may also need to add a copy constructor.
    Qt Code:
    1. adressdata::addressdata()
    2. : Xpos(0),
    3. Ypos(0),
    4. Zpos(0),
    5. address(""),
    6. name("")
    7. {}
    8. adressdata::adressdata(const adressdata& other)
    9. {
    10. ....
    11. }
    To copy to clipboard, switch view to plain text mode 
    Me, Grimlock, not "nice dino". ME BASH BRAINS!

  3. The following user says thank you to Grimlock for this useful post:

    Thomas Wrobel (22nd November 2009)

  4. #3
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use Q_DECLARE_METATYPE correctly with a custom class?

    Sorry double post
    Me, Grimlock, not "nice dino". ME BASH BRAINS!

  5. #4
    Join Date
    Nov 2009
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use Q_DECLARE_METATYPE correctly with a custom class?

    Quote Originally Posted by Grimlock View Post
    You are missing a default constructor in your class implementatnion.
    You may also need to add a copy constructor.
    Qt Code:
    1. adressdata::addressdata()
    2. : Xpos(0),
    3. Ypos(0),
    4. Zpos(0),
    5. address(""),
    6. name("")
    7. {}
    8. adressdata::adressdata(const adressdata& other)
    9. {
    10. ....
    11. }
    To copy to clipboard, switch view to plain text mode 
    Ok, thanks that works

    At least, I think so.
    I'm now getting a "error: no match for 'operator<<' in 's << (+l)->QList<T>::at [with T = addressdata](i)'", which I assume means I havnt defined what that << operator should do.

    I placed;

    inline QDataStream& operator<<(QDataStream& out, const addressdata& data)
    {
    out << data.Xpos;
    out << data.Ypos;
    out << data.Zpos;
    out << data.getName();
    out << data.getAddress();

    return out;
    }
    inline QDataStream& operator>>(QDataStream& in, addressdata& data)
    {
    in >> data.Xpos;
    in >> data.Ypos;
    in >> data.Zpos;
    in >> data.getName();
    in >> data.getAddress();
    return in;
    }
    Into the class definition of addressdata.
    This gives me this though;

    `QDataStream& addressdata:perator<<(QDataStream&, const addressdata&)' must take exactly one argument

    Seeing as I copyed the formatting from the wiki (with the two arguments), I'm assuming Ive played it in the wrong place? Should the inlines go here?

    Cheers,
    Thomas

  6. #5
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use Q_DECLARE_METATYPE correctly with a custom class?

    Declare them outside of your class and befriend the operators or just remove the seckond parameter and try using this-> instedof data.
    Me, Grimlock, not "nice dino". ME BASH BRAINS!

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

    Thomas Wrobel (22nd November 2009)

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use Q_DECLARE_METATYPE correctly with a custom class?

    You don't need to declare the metatype for your class to be able to stream it to QDataStream.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #7
    Join Date
    Nov 2009
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use Q_DECLARE_METATYPE correctly with a custom class?

    Quote Originally Posted by Grimlock View Post
    Declare them outside of your class and befriend the operators
    Not sure how to do befriending, let alone with operators.

    friend void QDataStream::??(QDataStream); ?

    Quote Originally Posted by Grimlock View Post
    or just remove the seckond parameter and try using this-> instedof data.
    I tried changing it to;

    Qt Code:
    1. inline QDataStream& operator<<(QDataStream& out)
    2. {
    3. out << this->Xpos;
    4. out << this->Ypos;
    5. out << this->Zpos;
    6. out << this->name;
    7. out << this->address;
    8.  
    9. return out;
    10. }
    11. inline QDataStream& operator>>(QDataStream& in)
    12. {
    13. in >> this->Xpos;
    14. in >> this->Ypos;
    15. in >> this->Zpos;
    16. in >> this->name;
    17. in >> this->address;
    18.  
    19. return in;
    20. }
    To copy to clipboard, switch view to plain text mode 

    in addressdata.h, but I still got;

    error: no match for 'operator<<' in 's << (+l)->QList<T>::at [with T = addressdata](i)


    I'm probably doing something really basic wrong.

  10. #8
    Join Date
    Nov 2009
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use Q_DECLARE_METATYPE correctly with a custom class?

    Quote Originally Posted by wysota View Post
    You don't need to declare the metatype for your class to be able to stream it to QDataStream.
    Dont I?
    What if I want to use my class with reading/witting to network connections, which is something I'll certainly want to learn how to do in future.

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use Q_DECLARE_METATYPE correctly with a custom class?

    Quote Originally Posted by Thomas Wrobel View Post
    Dont I?
    No. It's only for placing objects of your class as values of QVariant.

    What if I want to use my class with reading/witting to network connections, which is something I'll certainly want to learn how to do in future.
    You don't need Q_DECLARE_METATYPE. Just provide steaming operators to QDataStream if you want to use it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #10
    Join Date
    Nov 2009
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use Q_DECLARE_METATYPE correctly with a custom class?

    Ok, so I just gota learn how to set up my streaming operators correctly, thanks

  13. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use Q_DECLARE_METATYPE correctly with a custom class?

    The QDebug Class Reference has a good example of how to setup streaming operators so you can add custom types to debug output. The same applies to other objects such as QDataStream.

  14. #12
    Join Date
    Nov 2009
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use Q_DECLARE_METATYPE correctly with a custom class?

    Did you mean: http://doc.trolltech.com/4.5/qdebug.html ?

    Because I was already attempting to use the examples here (see above);
    http://wiki.qtcentre.org/index.php?t..._types_with_Qt
    Which seem better.

Similar Threads

  1. Connecting custom class actions
    By Toshikazu in forum Qt Programming
    Replies: 3
    Last Post: 15th October 2009, 15:31
  2. custom class for tslib
    By nrabara in forum Newbie
    Replies: 1
    Last Post: 28th April 2009, 13:15
  3. How to store custom class in QHash?
    By Misenko in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2008, 08:57
  4. QListWidget inheriting custom class
    By phannent in forum Qt Tools
    Replies: 1
    Last Post: 4th August 2008, 14:39
  5. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57

Tags for this Thread

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.