Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 7

Qt-interest Archive, April 2007
Pointers in QVariant?


Message 1 in thread

Hi,
I am missing the possibility to acces void* pointers by QVariant... it is not 
the example for good code to use something like that, but it helps a lot :-)
(Maybe on step better could be Pointers to QObjects, don't know...)

If you build a attr/value-Pair QMap<QString,QVariant>, collect a lot of them 
in a list, you have something like
[MyList]
1: name=first, id=1, ...
2: name=2nd, id=2
...

what I'd like to have is
1: ..., childs=0x3E87456
-> [MyChilds]
	1: name=child one, id=1...

if these lists (and sublists/trees) are loaded from and saved to xml nodes, I 
could do fantastic things without knowing whats inside...

Is there any hint or do I just have to add my own QVariant type "Ptr"? 

--
 [ signature omitted ] 

Message 2 in thread

"J. Preiss" <auba@xxxxxxx> wrote in message 
news:200704292250.01672.auba@xxxxxxxxxx
> Hi,
> I am missing the possibility to acces void* pointers by QVariant... it 
> is not
> the example for good code to use something like that, but it helps a lot 
> :-)
> (Maybe on step better could be Pointers to QObjects, don't know...)
>
> If you build a attr/value-Pair QMap<QString,QVariant>, collect a lot of 
> them
> in a list, you have something like
> [MyList]
> 1: name=first, id=1, ...
> 2: name=2nd, id=2
> ...
>
> what I'd like to have is
> 1: ..., childs=0x3E87456
> -> [MyChilds]
> 1: name=child one, id=1...
>
> if these lists (and sublists/trees) are loaded from and saved to xml 
> nodes, I
> could do fantastic things without knowing whats inside...
>
> Is there any hint or do I just have to add my own QVariant type "Ptr"?


You can use the meta type system to add types to QVariant, and that 
includes pointer-types. But if your data is serialized from XML, then it 
might be worth considering if the data is not best stored in value-types.


If you want to get a pointer to the object stored in the QVariant, then 
use QVariant::data and QVariant::constData (undocumented, but public 
functions).

Volker


--
 [ signature omitted ] 

Message 3 in thread

> You can use the meta type system to add types to QVariant, and that
> includes pointer-types. But if your data is serialized from XML, then it
> might be worth considering if the data is not best stored in value-types.

Oh, of course they are - if the access type is via files. Then I have a 
QHash<int,QVariant> at the end. But if one attribute is a pointer type, I 
want to get again a pointer to a QHash<int,QVariant>. After all, I get a tree 
structure.

> If you want to get a pointer to the object stored in the QVariant, then
> use QVariant::data and QVariant::constData (undocumented, but public
> functions).

So I only have to do a

	typedef  QHash<int,QVariant> MyList;
	qRegisterMetaType<MyList*>("Listptr");

or even only a 

	Q_DECLARE_METATYPE(MyList)

and thats all? Then I can do a

	void doIt(MyList* list)
	{
		QVariant var = list;
	}
	void orNot(QVariant& var)
	{
		MyList* list = var;
	}

? Would be really magic if so... 

--
 [ signature omitted ]