Sorting QtTableModel - QTableView doesn't get updated

c++, model-view, qt, sorting

Solution

D'oh!

I was ready to dig into the Qt Source Code. But as I single stepped through my code, I saw the cursor jumping to the return statement in the 'default' case.

I had just forgotten to add a 'break' to my switch-case! It was just a simple fall-through error :(( It works now perfectly fine with "layoutChanged".

Problem

I implemented a custom `QAbstractTableModel` and I'm using a `std::vector` for my data objects. Now I wanted to implement the sort() method, to get my table sorted by column. That's basically what I do: ``` void SBStateTableModel::sort (int column, Qt::SortOrder order) { emit layoutAboutToBeChanged(); switch (column) { case Address: if (order == Qt::DescendingOrder) std::sort(states.begin(), states.end(), addr_comp_desc); else std::sort(states.begin(), states.end(), addr_comp_asc); default: return; } emit layoutChanged(); } ``` But emitting `layoutChanged()` alone doesn't redraw the view. When mark a row and cycle through them, they get updated as they are being highlighted. The documentation also speaks about updating the persistent indexes. Some people on here have suggested, that this is in fact not necessary. I'm not even sure how to go about it. Getting the list with `persistentIndexList()` and then I have to sort it. But `std::sort` is not a stable sort. I'm not sure how to match the persistent indices with my vector indices. EDIT: There was just a "break" missing in the 'case'! So the function would return before emitting the layoutChanged signal.

Original source