PDA

View Full Version : QVariant custom/user type comparison



gri
12th August 2008, 08:23
Hello,

as the Qt docs state QVariant does not call the "operator==" when you compare two variants with custom types.

Is there any way to define a comparison method for these types? I want to put a custom type (boost::smart_ptr) into the QComboBox data which works well for enum's when you cast them to int, otherwise not - and the smart_ptr can't be casted to something else :( Putting this type into the ComboBox works well but i can't use QComboBox::findData() to find my items again ...


QVariant v1 = qVariantFromValue(TestNamespace::TestEnum_TestValu e1);
QVariant v11 = qVariantFromValue(TestNamespace::TestEnum_TestValu e1);

v1 == v11: false
v1 == v1 : true

Thanks for your help and sorry for the bad english :o

Edit:
One way would be to extend undocumented the QVariant::Handler like it's done in QtGui/qguivariant.cpp, but this can only be done from ::qRegisterGuiVariant() and I don't want to modify QVariant itself. Since Qt is not statically linked and qRegisterGuiVariant and qUnregisterGuiVariant are not exported from QtGui, I could "fake" them but that would be an ugly hack ...

gri
12th August 2008, 09:56
In attachment: The ugly hack solution ... I don't want to use this one! :eek:

gri
12th August 2008, 14:36
Ok, even if no one cares about this ... I'm posting my solution which is not a good example but acceptable :)