According to the Qt documentation, QVariant::operator==
does not work as one might expect if the variant contains a custom type:
bool QVariant::operator== ( const QVariant & v ) const
Compares this QVariant with v and returns true if they are equal; otherwise returns false.
In the case of custom types, their equalness operators are not called. Instead the values' addresses are compared.
How are you supposed to get this to behave meaningfully for your custom types? In my case, I'm storing an enumerated value in a QVariant, e.g.
In a header:
enum MyEnum { Foo, Bar };
Q_DECLARE_METATYPE(MyEnum);
Somewhere in a function:
QVariant var1 = QVariant::fromValue<MyEnum>(Foo);
QVariant var2 = QVariant::fromValue<MyEnum>(Foo);
assert(var1 == var2); // Fails!
What do I need to do differently in order for this assertion to be true?
I understand why it's not working -- each variant is storing a separate copy of the enumerated value, so they have different addresses. I want to know how I can change my approach to storing these values in variants so that either this is not an issue, or so that they do both reference the same underlying variable.
It don't think it's possible for me to get around needing equality comparisons to work. The context is that I am using this enumeration as the UserData in items in a QComboBox
and I want to be able to use QComboBox::findData
to locate the item index corresponding to a particular enumerated value.