Qt-interest Archive, February 2007
QTableWidget sorting problems
Message 1 in thread
Hello,
I'm experiencing problems using the setSortingEnabled() feature provided
since Qt 4.2.0.
I have a QTableWidget filled in with a list of celebrities. The first column
contains a status icon (granted G or forbidden F) and the firstname, and the
second column contains the lastname. The celebrities in this QTableWidget
can be sorted either by their firstname, lastname, or both. The celebrities
are inserted in this order: Donald Duck, Daffy Duck, Woody Woodpicker and
Mickey Mouse. Since I want to provide the sorting facility to my
QTableWidget, I've set up setSortingEnabled( true ). First problem: if I
don't give an initial sorting order (by omitting a sortItems() call), I'm
getting a garbaged display:
Firstname Lastname
F Woody Mouse
F Mickey Duck
F Mickey Duck
F Donald Mouse
Why? Is it not possible to enable the sorting facility without effectively
providing an initial sorting order, so that the QTableWidgetItems are
displayed as they are inserted?
In order to get a correct display, I now choose to sort on the lastname in
ascending order with
sortItems( 1, Qt::AscendingOrder ):
Firstname Lastname
F Daffy Duck
F Donald Duck
F Mickey Mouse
F Woody Woodpicker
Notice the order of the two first lines: it seems that the sorting on the
lastname has also automatically sorted in ascending order the firstnames!
OK, now I want my QTableWidget being sorted by both the firstname (in
descending order) and the lastname (in ascending order). I thus call
sortItems( 0, Qt::DescendingOrder ) and then
sortItems( 1, Qt::AscendingOrder ). The result is:
Firstname Lastname
F Donald Duck
F Daffy Duck
F Mickey Mouse
F Woody Woodpicker
Notice how the first two lines are now ordered. Fine.
Now, I want to change the right access of Daffy Duck from forbidden (F) to
granted (G). I thus recover item( 1, 0 ) and do a setIcon() on it with the
new icon reflecting the new status. The listing gets completely corrupted
once again.
From the Qt documentation, I found that "If you want to enable sorting in
your table widget, do so after you have populated it with items, otherwise
sorting may interfere with the insertion order (see setItem() for details)".
It seems that altering a field of a QTableWidgetItem has the same effect
than inserting a new QTableWidgetItem. So, if I understand correctly, once
I've set up the new icon, I must perform a new sortItems() to correctly
display the listing. And here's the problem: how do I know which sortItems()
msut be called? If I perform only a sortItem( 1, Qt::AscendingOrder ), I'm
not guaranteed to recover the original ordering. Indeed, how do I know for
example whether the firstname column was also ordered (in ascending or
descending order?) or not? Is there a way to recover the QTableWidgetItems
status before performing a change in a sorted QTableWidget?
Thanks for any suggestion,
Émeric
Message 2 in thread
Hello,
I'm experiencing problems using the setSortingEnabled() feature provided
since Qt 4.2.0.
I have a QTableWidget filled in with a list of celebrities. The first column
contains a status icon (granted G or forbidden F) and the firstname, and the
second column contains the lastname. The celebrities in this QTableWidget
can be sorted either by their firstname, lastname, or both. The celebrities
are inserted in this order: Donald Duck, Daffy Duck, Woody Woodpicker and
Mickey Mouse. Since I want to provide the sorting facility to my
QTableWidget, I've set up setSortingEnabled( true ). First problem: if I
don't give an initial sorting order (by omitting a sortItems() call), I'm
getting a garbaged display:
Firstname Lastname
F Woody Mouse
F Mickey Duck
F Mickey Duck
F Donald Mouse
Why? Is it not possible to enable the sorting facility without effectively
providing an initial sorting order, so that the QTableWidgetItems are
displayed as they are inserted?
In order to get a correct display, I now choose to sort on the lastname in
ascending order with
sortItems( 1, Qt::AscendingOrder ):
Firstname Lastname
F Daffy Duck
F Donald Duck
F Mickey Mouse
F Woody Woodpicker
Notice the order of the two first lines: it seems that the sorting on the
lastname has also automatically sorted in ascending order the firstnames!
OK, now I want my QTableWidget being sorted by both the firstname (in
descending order) and the lastname (in ascending order). I thus call
sortItems( 0, Qt::DescendingOrder ) and then
sortItems( 1, Qt::AscendingOrder ). The result is:
Firstname Lastname
F Donald Duck
F Daffy Duck
F Mickey Mouse
F Woody Woodpicker
Notice how the first two lines are now ordered. Fine.
Now, I want to change the right access of Daffy Duck from forbidden (F) to
granted (G). I thus recover item( 1, 0 ) and do a setIcon() on it with the
new icon reflecting the new status. The listing gets completely corrupted
once again.
From the Qt documentation, I found that "If you want to enable sorting in
your table widget, do so after you have populated it with items, otherwise
sorting may interfere with the insertion order (see setItem() for details)".
It seems that altering a field of a QTableWidgetItem has the same effect
than inserting a new QTableWidgetItem. So, if I understand correctly, once
I've set up the new icon, I must perform a new sortItems() to correctly
display the listing. And here's the problem: how do I know which sortItems()
msut be called? If I perform only a sortItem( 1, Qt::AscendingOrder ), I'm
not guaranteed to recover the original ordering. Indeed, how do I know for
example whether the firstname column was also ordered (in ascending or
descending order?) or not? Is there a way to recover the QTableWidgetItems
status before performing a change in a sorted QTableWidget?
Thanks for any suggestion,
Émeric