QTableWidgetItem子类运算符<()在对QTableWidget进行排序时从未被调用
QTableWidgetItem subclass operator<() never getting called when sorting QTableWidget
我想为QTableWidget
实现一个自定义排序,所以我将QTableWidgetItem
子类化,并重新实现了operator<()
函数。
class FloatFieldTableItem : public QTableWidgetItem
{
public:
FloatFieldTableItem(qreal theFloat) :
QTableWidgetItem(),
_float(theFloat)
{}
virtual bool operator<(FloatFieldTableItem const &other) const
{
return _float < other.float;
}
private:
qreal _float;
};
但由于某种原因,operator<()
函数从未被调用!
tableWidget->setItem(0, 0, new FloatFieldTableItem(0.1));
tableWidget->setItem(1, 0, new FloatFieldTableItem(0.3));
tableWidget->setItem(2, 0, new FloatFieldTableItem(6.1));
// This should result in a call to the operator<() function above
tableWidget->sortItems(0);
您的operator<()
的签名与QTableWidgetItem
的签名不匹配,因此即使它已被声明为虚拟的,它也不会以允许QTableWidget
调用它的方式进入vtable
。
签名QTableWidgetItem
的operator<()
将QTableWidgetItem
作为参数,因此您必须将其强制转换为FloatFieldTableItem
才能执行您想要的操作。
virtual bool operator<(QTableWidgetItem const &other) const
{
FloatFieldTableItem const *item
= dynamic_cast<FloatFieldTableItem const*>(&other);
if(item)
{
return _float < item->_float;
}
else
{
return QTableWidgetItem::operator<(other);
}
}
相关文章:
- 选择排序时交换函数调用的数量和完成的交换次数是否相同?
- 是否可以在调用链接器时强制 CMake 重新排序参数?
- 为什么此代码上的排序算法不调用类的交换版本?
- 如何在类中从排序调用比较器函数C++
- std::atexit 从全局对象的构造函数调用时的排序
- 在单个排序调用C++中对多个向量进行排序
- 从递归向后选择排序函数调用 max 和交换函数
- 构造函数的初始值设定项列表中的函数调用是否按顺序排序?
- 在hadoop 2.7.4的自定义排序类中调用C函数,给出Java.lang.unsatisfiedlinkerror
- std::具有自定义比较函数结果的排序函数错误:必须调用对非静态成员函数的引用
- C++11 快速排序终止调用,没有活动异常
- 调用selectionsort函数只对数组的一部分进行排序
- 调用自定义排序函数的次数
- 如何在main中调用选择排序函数
- 调用标准::排序
- 为什么排序调用比较函数的频率低于线性最小搜索算法
- 通过调用交换值对数组进行排序
- 调用函数中的这些评估是什么,这些评估未在被调用函数的主体之前专门排序
- QTableWidgetItem子类运算符<()在对QTableWidget进行排序时从未被调用
- C++对一个对象向量进行排序,得到错误C3867函数调用缺少参数列表