Reputation: 5
I'm using Qt 5.21.3. I have a QTableWidget that, when initially displayed, shows the rows in the order they were populated. I've set setSortingEnabled(true) and all rows are sorted properly when I click the column headers, but I'd like the table sorted on a specific column, in this case column 0, before showing the table but I can't for the life of me figure out how to accomplish it.
I've tried setting setSortingEnabled(false) prior to populating the table then to true before showing the table, but that appears to do nothing.
The way I'm getting around this now is to use std::sort to sort QList before populating the table, but this just seems like kludge to me and I'm not sure I can sort on an item other than the first if I need to.
Upvotes: 0
Views: 786
Reputation: 243907
You have to invoke the sort() method of the model:
tableWidget->model()->sort(0, Qt::AscendingOrder);
or sortItems() method:
tableWidget->sortItems(0, Qt::AscendingOrder);
after filling the table.
Upvotes: 0