Domanda

I have a QTableWidget with first column populated with checkable items, so I needed to overload those items to be able to sort them. Sorting works as expected when I click on header of that column (rows are sorted - first there are checked rows and then not checked).

The problem occurs when I run my GUI and do not click on header of any column to sort table and then do this:

tableWidget.setSortingEnabled(0); 
// check/uncheck some checkable items here
tableWidget.setSortingEnabled(1);

In that situation overridden __lt__ is called 100+ times, but I do not expect that because I didn't clicked on header of that column to sort. So, why __lt__ is called? Why it compares some checkable items even if I didn't clicked on header of that column to sort them?

Please help me, calling __lt__ consumes too much time when I have 30+ rows.

È stato utile?

Soluzione

From docs (C++ qt but applies):

sortingEnabled : bool

This property holds whether sorting is enabled.

If this property is true, sorting is enabled for the table. If this property is false, sorting is not enabled. The default value is false.

Note:. Setting the property to true with setSortingEnabled() immediately triggers a call to sortByColumn() with the current sort section and order.

And if you check docs of QHeaderView (which is queried by the widget to know the column to sort by) you can read:

int QHeaderView::sortIndicatorSection () const

Returns the logical index of the section that has a sort indicator. By default this is section 0.

See also setSortIndicator(), sortIndicatorOrder(), and setSortIndicatorShown().

And:

Qt::SortOrder QHeaderView::sortIndicatorOrder () const

Returns the order for the sort indicator. If no section has a sort indicator the return value of this function is undefined.

So you should be careful with this

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top