I have a QTableView
with a QAbstractTableModel
child instance set as the model. The child implements void sort(int columnt, Qt::SortOrder order)
. I set QTableView::sortEnabled(bool)
to true
so I can sort my table by clicking on the column header. When I click the header sorting finishes almost instantly but the table is only updated when I move the mouse to the table area. Do I need to emit some signal from sort()
? Or call a function to update the QTableView
?
Asked
Active
Viewed 640 times
0

Nazar554
- 4,105
- 3
- 27
- 38
-
2A bit tricky: do emit dataChanged( QModelIndex(), QModelIndex() ); after sorting – Dmitry Sazonov Sep 17 '13 at 16:14
-
@DmitrySazonov it seems to work! will you add an answer? – Nazar554 Sep 17 '13 at 16:16
-
Do you mind posting code for your sort function, iv got this far but dont know what to put in the overriden sort function and cant seem to find any info on this ... – AngryDuck Oct 07 '13 at 15:59
-
1@AngryDuck just use qSort on your data container with lessthan operator. implement lessthen operator depending on what criteria are you sorting. if you got any problems, make a new question. – Nazar554 Oct 08 '13 at 12:38
1 Answers
0
A bit tricky: do emit dataChanged( QModelIndex(), QModelIndex() ); after sorting.
I want to note, that passing invalid indexes is not documented, but I often use it in my projects. It works well and it's much faster than passing valid indexes with big ranges, because cause to update only visible area. But it may have some glitches with heavy delegates / custom widgets.
I tested on QTreeView - updating range of 10k elements takes about 1,5 sec (emitting dataChanged with valid indexes). And when I use invalid indexes signal is processed immideatly.

Dmitry Sazonov
- 8,801
- 1
- 35
- 61
-
1I found in Qt doc that you should use `emit layoutChanged()`. I don't know which way is faster. – Nazar554 Oct 08 '13 at 12:39
-
layoutChanged will me sended to all views, that are connected to model. It is usefull, when size of items should be recalculated. You may check this: http://stackoverflow.com/questions/16444558/how-to-force-qabstractitemview-recalculate-items-sizehints – Dmitry Sazonov Oct 08 '13 at 12:58