How to support comparisons for QVariant objects containing a custom type?

♀尐吖头ヾ 提交于 2019-11-29 09:07:29

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.

strelok.ndv

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).

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())
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!