How to sort data in QTableWidget?

后端 未结 5 1681
星月不相逢
星月不相逢 2020-12-06 10:31

I have a QTableWidget and the first column contains numbers from 1 to 1000. Now I need to sort the table based on this first column.

I\'m using the func

5条回答
  •  执笔经年
    2020-12-06 11:18

    The easiest way is probably to subclass QTableWidgetItem and then implement the < operator to be smart about the fact that you're sorting numbers and not strings.

    class MyTableWidgetItem : public QTableWidgetItem {
        public:
            bool operator <(const QTableWidgetItem &other) const
            {
                return text().toInt() < other.text().toInt();
            }
    };
    

    Then when you're populating your table you can pass it instances of your custom items that know how to sort themselves properly instead of the generic ones.

提交回复
热议问题