如何支持包含自定义类型的QVariant对象的比较? [英] How to support comparisons for QVariant objects containing a custom type?

查看:445
本文介绍了如何支持包含自定义类型的QVariant对象的比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Qt文档,如果变体包含自定义类型,则QVariant::operator==可能无法正常工作:

According to the Qt documentation, QVariant::operator== does not work as one might expect if the variant contains a custom type:

布尔QVariant :: operator ==(const QVariant& v)const

将此QVariant与v和 如果相等,则返回true;否则,返回true. 否则返回false.

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.

您应该如何使其对您的自定义类型有意义?就我而言,我将枚举值存储在QVariant中,例如

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.

在标题中:

enum MyEnum { Foo, Bar };

Q_DECLARE_METATYPE(MyEnum);

函数中的某处:

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.

我认为我不可能绕开平等比较的工作.上下文是,我正在将此枚举用作QComboBox中项目中的UserData,并且希望能够使用QComboBox::findData来定位与特定枚举值相对应的项目索引.

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.

推荐答案

显而易见的答案是使用var1.value<MyEnum>() == var2.value<MyEnum>()将数据强制转换为比较数据,但这需要您在比较时知道类型.看来在您的情况下这是可能的.

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.

如果仅使用枚举,还可以将其转换为int以便存储在QVariant中.

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

为了澄清有关搜索 QComboBox 的信息,它使用组合框的模型来查找数据.具体来说,它使用 match() 函数="http://doc.qt.nokia.com/4.6/qabstractitemmodel.html" rel ="nofollow noreferrer"> QAbstractItemModel 来检查是否相等.幸运的是,此函数是虚拟的,因此您可以在子类中覆盖它.

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.

这篇关于如何支持包含自定义类型的QVariant对象的比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆