我想为我实现一个自定义排序,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);