22
votes

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

3 Answers

15
votes

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.

4
votes

Try hack qvariant, define the function by prototype

typedef bool (*f_compare)(const Private *, const Private *);

and set it to qvariant handler; To work with qvariant qt use Handler:

struct Handler {
    f_construct construct;
    f_clear clear;
    f_null isNull;
  #ifndef QT_NO_DATASTREAM
    f_load load;
    f_save save;
 #endif
    f_compare compare;
    f_convert convert;
    f_canConvert canConvert;
    f_debugStream debugStream;
};

This example demonstrates how to hack qvariant debug output and convert to string. This is very simple example, and you need to extend it for you problem. "Identifier" is my custom type.

class HackVariant : private QVariant
{
public:
     static void hackIt() {
         origh = handler;
         Handler* h = new Handler;
         *h = *origh;
         h->convert = convert;
         h->debugStream = hackStreamDebug;
         handler = h;
     }

private:
     static bool convert(const QVariant::Private *d, QVariant::Type t, void *result, bool *ok)
     {
         //qDebug() << Q_FUNC_INFO << "type:" << d->type;
         if (d->type >= QVariant::UserType)
         {
             QString& str = *((QString*)result);
             Identifier* ident = (Identifier*)(constData(d));
             str = ident->toString();
         }
         else
             return origh->convert(d, t, result, ok);
         return true;
     }

     static void hackStreamDebug(QDebug dbg, const QVariant &v) {
         if (v.canConvert<Identifier>())
             dbg << v.value<Identifier>();
         else
             origh->debugStream(dbg, v);
     }

     static const Handler* origh;

     static const void *constData(const QVariant::Private *d)
     {
         return d->is_shared ? d->data.shared->ptr : reinterpret_cast<const void *>(&d->data.ptr);
     }

};

You have to create function and set it to handler. Don't forget call HackVariant::hackIt() in main.cpp before use (var1 == var2).

4
votes

Solution for Qt 5

Qt supports this out of the box since version 5.2. See QVariant::operator== and QMetaType::registerComparators.

Solution for Qt 4

If you're still using Qt 4 and can't (or don't want) to upgrade to Qt 5 yet, you can use the CustomVariantComparator class which I've written up for one of my projects.

You can use it as follows. Let's say we have a class Foo which implements operator== and should be used within a QVariant:

class Foo {
public:
    bool operator==(const Foo &other) { return ...; }
};
Q_DECLARE_METATYPE(Foo)

Then, simply put the Q_DEFINE_COMPARATOR macro next to the implementation of Foo (i.e. within the Foo.cpp file, but not within the Foo.h file):

Q_DEFINE_COMPARATOR(Foo)

Next, after constructing your QApplication (or QCoreApplication) instance, enable the custom variant comparator (this only has to be done once):

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    CustomVariantComparator::setEnabled(true);
    // more code...
}

Now, the following code snippet will work as expected (i.e. invoke Foo::operator==).

QVariant::fromValue(Foo()) == QVariant::fromValue(Foo())