PDA

View Full Version : QVariant with Custom Type



kasmanit
1st September 2012, 23:00
Hello Everyone,

I Have a custom type, let say MyClassA



class MyClassA
{
function a();
function b();
....
};
Q_DECLARE_METATYPE(MyClassA);


In my program I use a QCombobox, so i have the need of a QVariant, so I use



MyClassA a
QVariant var = QVariant::fromValue(a);


My Question is, once I get back the QVariant, with the signal "currentindexchange" for example, how can I use the function of the class MyClassA, on the QVariant?

ThanksYou

Added after 23 minutes:

Ok So I have Found a solution, and for now, before another solution I'm doing:




QVariant var2 = ui->comboBox_3->itemData(index);
MyClassA classA = var2.value<MyClassA>();


Hope this will help someone

tbscope
2nd September 2012, 09:13
You should read up on type casting. I suggest a good C++ book.

amleto
2nd September 2012, 13:48
You should read up on type casting. I suggest a good C++ book.

eh?:confused:

tbscope
2nd September 2012, 14:02
eh?:confused:

What are you confused about? Did I say something wrong?

amleto
2nd September 2012, 18:02
I dont see how casting is relevant since you cannot use simple casts to recover 'proper' types from a QVariant.