QT (c++语言) - QTableWidget 数字表头排序问题

引言

QTableWidget 表头排序是根据字符串进行排序,如果这个列中只有数字,就变成了这样的了。

image.png

解决方案

重写QTableWidgetItem类

class Item: public QTableWidgetItem
{

public:
    Item(QString str){
        this->setText(str);
    }
    bool operator< (const QTableWidgetItem &other) const
    {
        return (this->text().toInt() < other.text().toInt());
    }
};

调用时

new Item(QString("%1").arg(staticData[row].getCount())));

如果是字符串类型,就用原始的类,不要用重写的这个类,不然会排序失败的。

image.png

评论 0

还没有评论,来说点什么吧。

提交后需要审核才会公开。