0

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.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
Aleksandar
  • 3,541
  • 4
  • 34
  • 57

1 Answers1

5

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

trompa
  • 1,967
  • 1
  • 18
  • 26
  • Thanks for your answer. First of all, I've already tried `sortIndicatorSection()` before and after `setSortingEnable(1)`, both results were "7". I have 7 columns (0-6), so I guessed that default sorted section is not 0 but column out of table (section 7 in my case) although sorting behaves like it is 0. Maybe that is because I have tableWidget and not tableView? – Aleksandar Jul 12 '13 at 10:07
  • 1
    Anyway, your answer pointed me in right direction and I've found this: `void QHeaderView::setSortIndicator(int logicalIndex, Qt::SortOrder order)` - _"logicalIndex may be -1, in which case no sort indicator will be shown and the model will return to its natural, unsorted order."_ . This works for tableWidget also, putting that line before tableWidget is populated solved my problem. – Aleksandar Jul 12 '13 at 10:10
  • QTableWidget inherits from QTableView. But handles a model internally. Strange that 7 returned. glad to help anyways :) – trompa Jul 12 '13 at 17:59
  • I have one more related question, so if you have any idea, please suggest it: I have this `self.connect(self.tableWidget,SIGNAL("cellChanged(int, int)"), self.DoSomething)`, so when I check/uncheck item `DoSomething` is called where I disable sorting, do some calculations and then enable sorting again. Every time I check or uncheck any item, setSortingEnabled(1) consume more and more time, and I don't know how to follow up what is happening. (for example 0.031999 0.041 0.04799 0.05299 .....) – Aleksandar Jul 15 '13 at 08:57