PDA

View Full Version : add custom class object to QVariantList



ggdev001
25th February 2013, 12:43
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:


items.append(&currentItem);
where items is of type QVariantList and currentItem is of Type CMyClass.

any help??

alizadeh91
25th February 2013, 13:06
MyClass *_mc = new MyClass();
items.appen(mc);

is NOT working?!

lanz
25th February 2013, 13:09
First of all, you need to declare your custom type(http://qt-project.org/doc/qt-4.8/qmetatype.html#Q_DECLARE_METATYPE):

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.

QVariant var;
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.

ggdev001
25th February 2013, 13:18
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

alizadeh91
25th February 2013, 13:22
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

lanz
25th February 2013, 13:24
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.

alizadeh91
25th February 2013, 13:25
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

ggdev001
25th February 2013, 13:29
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/qmetatype.html#Q_DECLARE_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)....

alizadeh91
25th February 2013, 13:31
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.

ggdev001
25th February 2013, 13:37
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....?

alizadeh91
25th February 2013, 13:38
QVariantList is list of QVariant. So your class have to be type of QVariant. You can't add anything to QVariantList

ggdev001
25th February 2013, 13:53
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:


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++)
{
QVariant currentVariant;
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.

alizadeh91
25th February 2013, 14:03
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

ggdev001
25th February 2013, 14:25
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 ...

alizadeh91
25th February 2013, 14:26
Yes that's correct :)

lanz
26th February 2013, 05:07
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

ggdev001
26th February 2013, 06:53
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) ...

lanz
26th February 2013, 07:16
#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


template <typename T>
inline void qVariantSetValue(QVariant &v, const T &t)
{
...
const uint type = qMetaTypeId<T>(reinterpret_cast<T *>(0));
...