QTableWidget 中的自定义排序 [英] Custom Sorting in QTableWidget

查看:109
本文介绍了QTableWidget 中的自定义排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QTableWidget,我通过标题列使用它的默认排序功能,但我在 QTableWidget 中的一列是整数类型,并且通过 QTableWidget 默认排序它像字符串一样排序.所以有任何方法我可以使用我自己的 QTableWidget 排序功能?

I have a QTableWidget and i am using its default sorting capability through header columns but one of my column in QTableWidget is integer type and through QTableWidget default sorting it is being sorted like a string.So there is any means by which i can use my own sorting functions for QTableWidget?

推荐答案

您可以尝试子类化 QTableWidgetItem 并重新实现它的 operator<().比在你的 QTableWidget 中使用这个自定义项目而不是默认的 QTableWidgetItems.像这样:

You can try to subclass the QTableWidgetItem and reimplement operator<() of it. Than in your QTableWidget use this custom items instead of default QTableWidgetItems. Something like this:

class Item: public QTableWidgetItem
{

 public:
     [..]
     bool operator< (const QTableWidgetItem &other) const
     {
         // TODO: To be safe, check weather conversion to int is possible.
         return (this->text().toInt() < other.text().toInt());
     }
     [..]
 };

在您的表格小部件中:

[..]
QTableWidgetItem *newItem = new Item("1");
tableWidget->setItem(row, column, newItem);
[..]

这篇关于QTableWidget 中的自定义排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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