[SOLVED] QTableView::setSortingEnabled() doesn't really work
-
Dear Qt community,
I'm facing a small problem with setSortingEnabled() method of QTableView class. I'm using a standard QTableWidget with selectionMode set to ExtendedSelection and selectionBehavior set to SelectRows. First column items are something like this:
@QTableWidgetItem* _pDateTime = new QTableWidgetItem();
_pDateTime->setFlags(_pDateTime->flags() ^ Qt::ItemIsEditable);
_pDateTime->setData(Qt::EditRole, QDateTime::fromString(json["dateTime"].toString(), "dd.MM.yyyy HH:mm:ss"));@Before setting all QTableWidgetItem's in the table I disable sorting by calling setSortingEnabled(false);. Now as soon as all items are set in position I'm re-enabling sorting but it does nothing. In the docs it says:
Note: . Setting the property to true with setSortingEnabled() immediately triggers a call to sortByColumn() with the current sort section and order.
I guess there's a problem with the current sort section - how can I change it to be column 0? I've tried doing like this:
@tableWidget->horizontalHeader()->setSortIndicator(0, Qt::DescendingOrder);@
but this only crashes the table widget (meaning that it shows the items only in the first column). Any ideas?
Thank you in advance.
Edit: I just noticed one weird thing. My table widget has 12 columns and the last one is hidden. If I call:
@tableWidget->horizontalHeader()->setSortIndicator(11, Qt::DescendingOrder);@
it actually sorts the table without crashing the widget.
-
Aloha,
I'm just guessing here, but you can always try to call the "sortByColumn":http://qt-project.org/doc/qt-5.1/qtwidgets/qtableview.html#sortByColumn method directly. And have the sorting enabled always.
If you do it directly you can specify which column to use.
Happy Coding
-
I ran some tests and it seems that setSortingEnabled(true) does try to sort the table so calling sortByColumn() manually does the job but significantly increases table setup.
I want to have this time as small as possible. If I don't turn sorting off and on between item insertion then the time to setup the table is around 340ms. If I have it turned off and on it increases to 380ms and if I manually call sortByColumn() it goes up to 420ms.
I have to disable sorting during item insertion because otherwise it's going to mess things up. I'm inserting the items in a loop to each row and docs suggest to turn sorting off for that.
PS: Of course I'm disabling sorting before any item is inserted and enabling it after all items are inserted.
-
Aah, I understand your point, indeed, the approach I provided is definitely not the best approach, why execute something twice if you can do it once.
Again just guessing, but maybe, just maybe you could "set the current index":http://qt-project.org/doc/qt-5.1/qtwidgets/qabstractitemview.html#setCurrentIndex to 0th columns, before enabling the sorting.
OR maybe even easier way, would be to call "setCurrentCell ":http://qt-project.org/doc/qt-5.1/qtwidgets/qtablewidget.html#setCurrentCellmethod method from QTableWidget and set cell to 0,0
I hope it helps