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:
Code:
items.append(¤tItem);
where items is of type QVariantList and currentItem is of Type CMyClass.
any help??
Re: add custom class object to QVariantList
MyClass *_mc = new MyClass();
items.appen(mc);
is NOT working?!
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):
Code:
Q_DECLARE_METATYPE (CustomType);
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.
Code:
var.setValue (currentItem);
lst.append (var);
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.
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
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
Re: add custom class object to QVariantList
Quote:
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.
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
Re: add custom class object to QVariantList
Quote:
Originally Posted by
alizadeh91
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)....
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.
Re: add custom class object to QVariantList
Quote:
Originally Posted by
alizadeh91
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....?
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
Re: add custom class object to QVariantList
Quote:
Originally Posted by
alizadeh91
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:
Code:
QVariantList temp = networkResponse.value("messages").toList(); // Retrieve QVariantList from somewhere else
CustomClass currentItem; // our custom class objects
for(int i = 0; i<temp.size(); i++)
{
currentItem.parse(temp.at(i).toMap()); // initialize our custom objects
currentVariant.setValue(currentItem); // store them in QVariant
items.append(currentVariant); // add QVariant to our QVariantList - which is *items*
}
Is the code OK? thanks.
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
Re: add custom class object to QVariantList
Quote:
Originally Posted by
alizadeh91
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 ...
Re: add custom class object to QVariantList
Re: add custom class object to QVariantList
Quote:
Originally Posted by
ggdev001
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. :D
Re: add custom class object to QVariantList
Quote:
Originally Posted by
lanz
Are you sure that you aren't using Q_DECLARE_METATYPE? Because I can't compile it without this macro. :D
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) ...
Re: add custom class object to QVariantList
Code:
#define Q_DECLARE_METATYPE(TYPE) \
QT_BEGIN_NAMESPACE \
template <> \
struct QMetaTypeId< TYPE > \
{ \
enum { Defined = 1 }; \
static int qt_metatype_id() \
{ \
...
} \
}; \
QT_END_NAMESPACE
As you can see this macro specializes template class QMetaTypeId, which is indirectly used inside QVariant
Code:
template <typename T>
inline void qVariantSetValue
(QVariant &v,
const T
&t
) {
...
const uint type = qMetaTypeId<T>(reinterpret_cast<T *>(0));
...