tags:

views:

206

answers:

1

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.

+3  A: 

The obvious answer is to cast the data out of with var1.value<MyEnum>() == var2.value<MyEnum>() to compare them, but that requires you to know the type when comparing. It seems like in your case this might be possible.

If you are just using enums, you could also convert it to an int for storage in the QVariant.

Edit: For clarification about searching a QComboBox, it uses the model of the combo box to find the data. Specifically, it uses the match() function of the QAbstractItemModel to check for equality. Luckily, this function is virtual so you can override it in a subclass.

Adam W
The obvious answer doesn't work because I can't change what `QComboBox` does inside of its `findData` method. And yes, I know I could just store it as an int, but that seems inelegant. Plus, while I'm using an enum right now, I might want to use a class in the same way later, so I'm looking for a general solution.
Tyler McHenry
Then you can override the `match()` function (http://doc.qt.nokia.com/4.6/qabstractitemmodel.html#match) in a model class and assign that to the `QComboBox` since that is how it does the comparisons for `findData` Edit: See here: http://qt.gitorious.org/qt/qt/blobs/4.7/src/gui/widgets/qcombobox.cpp#line1490
Adam W
That looks very promising. I'll come back and accept this answer if it works, once I get a chance to try it out.
Tyler McHenry
Worked great! I subclassed `QStandardItemModel` with a class templated on type `T`, and re-implemented `match` so that if an exact match was being searched for and the variant to find was convertible to `T`, the `match` method would compare the `.value<T>()` of the variants rather than the variants themselves. Thanks!
Tyler McHenry
Glad it helped, not too long ago I realized QComboBox could reference QAbstractItemModel, it made many interfaces much easier!
Adam W