Update
I have created an qt bugticket hoping the documentation will be extended.
Original Question
Believing an Question from 2010 and the Qt Documentation, the operator==() doesn't work with custom types.
Quote:
bool QVariant::operator==(const QVariant & v) const
Compares this QVariant with
vand returnstrueif they are equal; otherwise returnsfalse.
QVariantuses the equality operator of the type() it contains to check for equality.QVariantwill try toconvert()vif its type is not the same as this variant's type. SeecanConvert()for a list of possible conversions.Warning: This function doesn't support custom types registered with
qRegisterMetaType().
I've tried to reproduce the repro case from the Stackoverflow Question from 2010 and the comparison worked without any problems for me.
I also went a step further and tried comparisons using an own class which also worked perfectly. To reproduce, put the following code into any header:
enum MyEnum { Foo, Bar };
Q_DECLARE_METATYPE(MyEnum)
class MyClass
{
int value;
public:
MyClass() : value(0)
{
}
MyClass(int a) : value(a)
{
}
bool operator==(const MyClass &) const
{
Q_ASSERT(false); // This method seems not to be called
return false;
}
bool operator!=(const MyClass &) const
{
Q_ASSERT(false); // This method seems not to be called
return true;
}
};
Q_DECLARE_METATYPE(MyClass)
And the following code into any function:
QVariant var1 = QVariant::fromValue<MyEnum>(Foo);
QVariant var2 = QVariant::fromValue<MyEnum>(Foo);
Q_ASSERT(var1 == var2); // Succeeds!
var1 = QVariant::fromValue<MyEnum>(Foo);
var2 = QVariant::fromValue<MyEnum>(Bar);
Q_ASSERT(var1 != var2); // Succeeds!
QVariant obj1 = QVariant::fromValue<MyClass>(MyClass(42));
QVariant obj2 = QVariant::fromValue<MyClass>(MyClass(42));
Q_ASSERT(obj1 == obj2); // Succeeds!
obj1 = QVariant::fromValue<MyClass>(MyClass(42));
obj2 = QVariant::fromValue<MyClass>(MyClass(23));
Q_ASSERT(obj1 != obj2); // Succeeds!
I would guess that in newer qt versions the size of a type is aquired when the Q_DECLARE_METATYPE is used so the QVariant can compare values of unknown types bytewise.
But that's only a guess and I don't want to risk the stability of my application by guessing what qt does instead of relying on the documentation.
Can I find out, how the QVariant compares unknown types? I would prefer relying on specification than on implementation.
qRegisterMetaType()onMyEnumandMyClass? - ZaiborgqRegisterMetaType(). There are no code lines using MyClass or MyEnum except the lines I've posted above. - Random CitizenQVariant::value<T>()) also what is for example the type ofvar1? (or to be clear, what does the variable say what type it is) - Zaiborghave you tried to cast the values back to its original type bevore comparing? (QVariant::value<T>())That's actually what I am trying to avoid. I am programming a property widget and I want to be able to compare properties between multiple objects to know which widgets to enable, whether multiple selected objects have the same value... So using an abstract way to compare values is very desirable for my use-case. - Random CitizenQVariantdoes the right thing, you can try to find out wat is going on or live with the black magic behind the scenes - Zaiborg