Swapping columns on QTreeView causes row highlight to disconnect
-
I'm facing a visual issue with QTreeView and am wondering if anyone knows a fix for it. I'm swapping the first column ("Name") and second column ("#") in my QTreeView for reasons explained below. The code I'm using to do this is:
myTreeView->header()->swapSections(0, 1);I've also tried calling
moveSection()on the header instead. Either way, the columns do get moved as expected. However, the highlighting on the rows is then visually disconnected between the two columns:
Without swapping, the highlighting is connected, as desired:

Is there a way I can make it so that the highlighting is connected even when the columns are swapped?
The reason I'm swapping these columns is because I'm reusing a model for several different collections of data. Some collections are flat, as shown above, and some collections are hierarchical, as shown below:

In the case of a flat collection, I do care about the data in the "#" column, and I want to display that column in the left-most position to help the user think about it as an index. But in the case of a hierarchical collection, I want to hide that data from the user, since indexing in a hierarchical context isn't quite so simple. But, as far as I am aware, in order to be displayed correctly by a QTreeView, a QAbstractItemModel needs its indexes to always be parented on the left-most column, and it needs that column to not be hidden. So the "#" column can't be the parent column, and it can't be the left-most column (at least not logically). So instead, I'm making the "Name" column be the parent column and putting it in the left-most position (logically). I thought I could just rearrange the columns to make the "#" be left-most (visually) in the case of a flat collection, but that has the issue described above.
-
Moving the first row around in a tree view is generally not a good idea because it is a special row with decorations. There are some bugs around wrt to this.
Use a QSortFilterProxyModel or similar to change the contents or do it directly in the source model. -
Moving the first row around in a tree view is generally not a good idea because it is a special row with decorations. There are some bugs around wrt to this.
Use a QSortFilterProxyModel or similar to change the contents or do it directly in the source model.@Christian-Ehrlicher Thanks for your response. I was worried that might be the case. Changing the model or implementing a proxy model will be a bigger change to the code, but should be doable.
-
P peter.thompson has marked this topic as solved on