Results 1 to 18 of 18

Thread: add custom class object to QVariantList

  1. #1
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default add custom class object to QVariantList

    Hello,

    I am trying to add a custom class object to my QVariantList
    but I get an error: error: 'QVariant::QVariant(void*)' is private

    e.g., here is the code I use:

    Qt Code:
    1. items.append(&currentItem);
    To copy to clipboard, switch view to plain text mode 
    where items is of type QVariantList and currentItem is of Type CMyClass.

    any help??

  2. #2
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: add custom class object to QVariantList

    MyClass *_mc = new MyClass();
    items.appen(mc);

    is NOT working?!

  3. #3
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: add custom class object to QVariantList

    First of all, you need to declare your custom type(http://qt-project.org/doc/qt-4.8/qme...ARE_METATYPE):
    Qt Code:
    1. Q_DECLARE_METATYPE (CustomType);
    To copy to clipboard, switch view to plain text mode 
    EDIT: And then provide copy constructor and default constructor for that type (that it become a value type)
    Then you need to store your value inside QVariant, then inside the list.
    Qt Code:
    1. var.setValue (currentItem);
    2. lst.append (var);
    To copy to clipboard, switch view to plain text mode 
    But it all seems like you're using wrong tool.

    Second, it may be not a very good idea to store pointers at QVariant. It depends on how you want to use this list though.
    Last edited by lanz; 25th February 2013 at 13:18.

  4. #4
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: add custom class object to QVariantList

    why should it matter how I should use the list??

    I just have my own class and I want to be able to store its objects in QVariantList

  5. #5
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: add custom class object to QVariantList

    You don't need to declare your type.
    Substitution way : you can have QList<yourType*> and then add your objects into list. (QVariantList is a List of QVariants, It has to be OK with QVariantList also!) but QList<T> is better way to store your objects in list

  6. #6
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: add custom class object to QVariantList

    why should it matter how I should use the list??
    I'm trying to see design decisions behind placing your objects into QVariantList, because you're storing pointers in QVariants, and that's feels wrong.

    If you want to put objects of your class into QVariant, see my edit to the earlier post.

  7. #7
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: add custom class object to QVariantList

    There are many container classes in Qt that you may like them and all of them can store objects. see this: http://qt-project.org/doc/qt-4.8/containers.html

  8. #8
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: add custom class object to QVariantList

    Quote Originally Posted by alizadeh91 View Post
    You don't need to declare your type.
    Substitution way : you can have QList<yourType*> and then add your objects into list. (QVariantList is a List of QVariants, It has to be OK with QVariantList also!) but QList<T> is better way to store your objects in list
    from here: http://qt-project.org/doc/qt-4.8/qme...CLARE_METATYPE though it seems I have to.

    ps. Can someone please tell me where in the .h or .cpp file I have to use the Q_DECLARE_METATYPE ??
    pps. @alizadeh91: thanks -- but I am first trying to make it work with QVariantList but I get error I mentioned above. Could you please post a line snippet maybe which would successfully add my custom class object to the QVariantList?? Thanks.

    I just want to be able to store an array of my Classes objects somewhere. I thought QVariantList is a good idea because I use it in other places too (e.g., usually when you parse JSON file the root object maybe a QVariantList and that's why I used it in other places)....

  9. #9
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: add custom class object to QVariantList

    As i know for custom objects, you can easily add it to container classes. for example suppose you have class name MyClass:
    MyClass *_mc = new MyClass();
    QList<MyClass*> list;list.append(_mc);

    If you are sure about type conversion between your class and QVariant then you can add this class object into QVariantList too.

  10. #10
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: add custom class object to QVariantList

    Quote Originally Posted by alizadeh91 View Post
    As i know for custom objects, you can easily add it to container classes. for example suppose you have class name MyClass:
    MyClass *_mc = new MyClass();
    QList<MyClass*> list;list.append(_mc);

    If you are sure about type conversion between your class and QVariant then you can add this class object into QVariantList too.
    OK, I got the example how to add custom object to QList. Now I would be interested how to add same custom objects to QVariantList....?

  11. #11
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: add custom class object to QVariantList

    QVariantList is list of QVariant. So your class have to be type of QVariant. You can't add anything to QVariantList

  12. #12
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: add custom class object to QVariantList

    Quote Originally Posted by alizadeh91 View Post
    QVariantList is list of QVariant. So your class have to be type of QVariant. You can't add anything to QVariantList
    I think I made it work. This is how my code looks when I want to add custom objects to QVariantList without using Q_DECLARE_METATYPE:
    Qt Code:
    1. QVariantList temp = networkResponse.value("messages").toList(); // Retrieve QVariantList from somewhere else
    2. CustomClass currentItem; // our custom class objects
    3. for(int i = 0; i<temp.size(); i++)
    4. {
    5. QVariant currentVariant;
    6. currentItem.parse(temp.at(i).toMap()); // initialize our custom objects
    7. currentVariant.setValue(currentItem); // store them in QVariant
    8. items.append(currentVariant); // add QVariant to our QVariantList - which is *items*
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    Is the code OK? thanks.

  13. #13
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: add custom class object to QVariantList

    seems little messy! But remember that QVariant has safe type conversion. for example you can add string, integer or any type that is compatible with qvariant into it. It will be converted safely. see this : http://qt-project.org/doc/qt-4.8/qvariant.html

  14. #14
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: add custom class object to QVariantList

    Quote Originally Posted by alizadeh91 View Post
    seems little messy! But remember that QVariant has safe type conversion. for example you can add string, integer or any type that is compatible with qvariant into it. It will be converted safely. see this : http://qt-project.org/doc/qt-4.8/qvariant.html
    at this point I am just interested if it is correct or not ...

  15. #15
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: add custom class object to QVariantList

    Yes that's correct

  16. The following user says thank you to alizadeh91 for this useful post:

    ggdev001 (25th February 2013)

  17. #16
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: add custom class object to QVariantList

    Quote Originally Posted by ggdev001 View Post
    I think I made it work. This is how my code looks when I want to add custom objects to QVariantList without using Q_DECLARE_METATYPE:
    Are you sure that you aren't using Q_DECLARE_METATYPE? Because I can't compile it without this macro.

  18. #17
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: add custom class object to QVariantList

    Quote Originally Posted by lanz View Post
    Are you sure that you aren't using Q_DECLARE_METATYPE? Because I can't compile it without this macro.
    Yes I didn't add it anywhere.....;

    edit: Sorry you were right indeed it seems I had added that MACRO on the top of cpp file (just forgot it) ...

  19. #18
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: add custom class object to QVariantList

    Qt Code:
    1. #define Q_DECLARE_METATYPE(TYPE) \
    2. QT_BEGIN_NAMESPACE \
    3. template <> \
    4. struct QMetaTypeId< TYPE > \
    5. { \
    6. enum { Defined = 1 }; \
    7. static int qt_metatype_id() \
    8. { \
    9. ...
    10. } \
    11. }; \
    12. QT_END_NAMESPACE
    To copy to clipboard, switch view to plain text mode 

    As you can see this macro specializes template class QMetaTypeId, which is indirectly used inside QVariant

    Qt Code:
    1. template <typename T>
    2. inline void qVariantSetValue(QVariant &v, const T &t)
    3. {
    4. ...
    5. const uint type = qMetaTypeId<T>(reinterpret_cast<T *>(0));
    6. ...
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 16:15
  2. Problem in using UI object in other class
    By beginQT in forum Qt Programming
    Replies: 4
    Last Post: 19th January 2012, 11:53
  3. Replies: 7
    Last Post: 18th August 2011, 14:43
  4. Accessing a class Object by pointer in another class
    By pdoria in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2008, 15:59
  5. Replies: 3
    Last Post: 16th May 2007, 11:07

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.