I need to use Qvariant to store and retrieve a simple int array. I can't....
I have declare an int * my_array;
If I use vv.value<int*>();
I have qt_metatype_id' is not a member of 'QMetaTypeId<int*>'
Any help ? Thanks
I need to use Qvariant to store and retrieve a simple int array. I can't....
I have declare an int * my_array;
If I use vv.value<int*>();
I have qt_metatype_id' is not a member of 'QMetaTypeId<int*>'
Any help ? Thanks
"int*" is "address of an int value". Do you really want to store a pointer in the variant? If so, then remember that an address is also a number and you can treat it as one and tell QVariant to treat it as one as well. Not an elegant solution but it will work.
But int * my_array and my_array = new int[5] .....
I'd can use a fixed or dynamc int array, simply I was testing how to get that the QVariant gives me the value...
I'm unable to store and retrieve a simple int[5] . into/from a Qvariant .....
Can you help me?
Thanks
I don't really want to teach you C++ right now. Get a book and read it.
I already told you, treat the address as an integer, QVariant can accept those.Can you help me?
tonnot (21st September 2011)
Ok, I'm going to try it
QVariant will accept a void*, so if you are willing to discard type safety:
or, you can declare int* to the meta-type system:Qt Code:
int *my_array = new int[5]; int *p1 = static_cast<int*>( v1.value<void*>() ); delete[] my_array;To copy to clipboard, switch view to plain text mode
Qt Code:
Q_DECLARE_METATYPE(int*) // and then int *p2 = v2.value<int*>();To copy to clipboard, switch view to plain text mode
Before you run off passing the pointer... you are passing a pointer to a block of memory (that might not exist) without passing the size of the block. How is the receiver supposed to know how many elements (ints in this case) can be safely accessed through that pointer?
Have you considered using QVector<int>?
This will copy the data if you modify the data at the receiving end but for read-only purposes is quite efficient.Qt Code:
Q_DECLARE_METATYPE(QVector<int>) QVector<int> my_array(5); QVector<int> a = v3.value<QVector<int> >(); qDebug() << a.size();To copy to clipboard, switch view to plain text mode
tonnot (22nd September 2011)
Thank you very much !!!
A very vell explanation !
Bookmarks