all.west
all.west

Reputation: 81

Sorting QTableWidget by column

I have a QTableWidget and I want to sort the components of this table according to the values of the zero column, I used this code :

ui->tableParticle->sortByColumn(0,Qt::AscendingOrder);

but it gives no good results, the image below shows you the problem I sought solution but found nothing, sorry I can't put all the code because it's very long.

Image

Upvotes: 3

Views: 20533

Answers (3)

Seleznev Anton
Seleznev Anton

Reputation: 661

By deafault QTableWidget sorts columns as ordinary strings. If you search for questions like "custom sorting in QTableWidget", you will find solutions to your answer, e.g.

  1. Custom sorting method of QTableView?

  2. Custom Sorting in QTableWidget

Upvotes: 3

all.west
all.west

Reputation: 81

I find also this answer add by SoloPilot, but i dont know how the pad the number strings with blanks and when I will add the line (' '+numStr)[-4:] :

The answer

One way that worked in my situation was

  1. before filling the table, turn off sorting:

table.setSortingEnabled(False)

  1. pad the number strings with blanks and make all strings in the column have the same length:

('    '+numStr)[-4:]

  1. after filling the table, turn on sorting:

table.setSortingEnabled(True)

This fixed the row sorting problem and the numerical order.

Upvotes: 2

all.west
all.west

Reputation: 81

I add that before setItem and it's work :

ui->table->horizontalHeader()->sortIndicatorOrder();

Upvotes: -1

Related Questions