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?
3 Answers
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());
}
[..]
};
And in your table widget:
[..]
QTableWidgetItem *newItem = new Item("1");
tableWidget->setItem(row, column, newItem);
[..]

- 20,808
- 3
- 47
- 55
-
1I tried this. It doesn't work. It seems that QTableWidgetItem::operator <() is never called when the QTableWidget is sorted. – Andy Brice Nov 26 '14 at 23:56
-
2My mistake. I overrode this: bool operator< ( const MyTableWidgetItem& rhs ) const; , when I should have overridden this: bool operator< ( const QTableWidgetItem& rhs ) const; . My mistake. – Andy Brice Nov 26 '14 at 23:58
-
@AndyBrice THANK YOU SO MUCH FOR THIS! I had made the same mistake and thanks to you I finally found it! – Ian Rehwinkel Apr 30 '21 at 08:56
I am not sure, but I don't think there is an easy way to change the sorting behaviour of a QTableWidget.
QTableWidget is just a convenience class for QTableView, which uses a default model. No guarantee, but what would try to do:
QTableWidget inheris the model() method from QTableView. With it you should be able to get the widget's model:
QAbstractItemModel *model = yourTableWidget->model();
This was the easy part. You now need a custom QSortFilterProxyModel
, where you can override the virtual bool lessThan(const QModelIndex & left, const QModelIndex & right) const
method.
And finally:
YourCustomFilterProxyModel *proxyModel = new YourCustomFilterProxyModel(this);
proxyModel->setSourceModel(model);
yourTableWidget->setModel(proxyModel);
No guarantee in so far that I never tried to replace the default model in a QTableWidget. If possible you should look into the Qt views and models. Initially they look harder to use, but it pays to get comfortable with them. IMHO QTableWidget is just an ancient relict from Qt3.

- 3,935
- 2
- 17
- 28
-
-
@AntonN.Petrov Private virtual function isn’t a problem. It’s public in the base class. So calling this function through the base class pointer shouldn’t be a problem at all. – ovanes Mar 23 '23 at 00:15
Are you sure it didn't sort the data well? Make sure you do add number there, not string. So, to add 40 to QTableWidget row, you use this data:
36: {'firstname': 'b', 'lastname': '111', 'email': 'foo@gmail.com',
'affiliate': 'Stuart Little', 'total_account_value': 40},
Instead of this:
36: {'firstname': 'b', 'lastname': '111', 'email': 'foo@gmail.com',
'affiliate': 'Stuart Little', 'total_account_value': '40'},
QTableWidget will recognize it as integer and will sort it well

- 4,997
- 8
- 64
- 106
-
1when I insert integer values to the QTableWidget it doesn't show the values at all – Oshada Oct 02 '18 at 05:25