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

Qt-interest Archive, July 2007
How to set a void* data in QVariant ?


Message 1 in thread

Hi Everyone,
I need to set my own object pointer to QVariant. My own object is not
a QT class/object. I use following code to set the data into QVariant :

MyOwnClass* pMyPointer=_GetMyObject();

QVariant myVar=QVariant(QVariant::UserType, pMyPointer);

I'm not sure if I do the right way.

Most important of all, I don't know how to get the pointer back from
the QVariant. I use

MyOwnClass* pGetBackPointer=myVar.value<MyOwnClass*>();

This will make failed in GCC.

So now I'm stuck here. Please help me out.

Thanks.

Regards,
Pagan


--
 [ signature omitted ] 

Message 2 in thread

Of course we know void * is wrong ;)  So there must be a qt way :)

For a given class XXXXX
Call the macro
Q_DECLARE_METATYPE( XXXXX * );

Note the macro is declared in <QVariant> and class XXXXX must be fully
defined at the macro call, I usually call it right after the class is
defined

Ie
Class XXXXX
{
....
};
Q_DECLARE_METATYPE( XXXXX * );


Then, when you want to create a QVariant of that type call
qVariantFromValue() with the variable to be made into a variant

ie

XXXXX * value = new value();
QVariant var = qVariantFromValue( value );

Then to get the value back call the value method on the variant

XXXXX * otherValue = var.value< XXXXX * >();


Good luck.
Scott

> -----Original Message-----
> From: Pagan Chou [mailto:pagan_chou@xxxxxxxxxxxxxx]
> Sent: Sunday, July 01, 2007 7:25 PM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: How to set a void* data in QVariant ?
> 
> Hi Everyone,
> I need to set my own object pointer to QVariant. My own object is not
> a QT class/object. I use following code to set the data into QVariant
:
> 
> MyOwnClass* pMyPointer=_GetMyObject();
> 
> QVariant myVar=QVariant(QVariant::UserType, pMyPointer);
> 
> I'm not sure if I do the right way.
> 
> Most important of all, I don't know how to get the pointer back from
> the QVariant. I use
> 
> MyOwnClass* pGetBackPointer=myVar.value<MyOwnClass*>();
> 
> This will make failed in GCC.
> 
> So now I'm stuck here. Please help me out.
> 
> Thanks.
> 
> Regards,
> Pagan
> 
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ]