@mjiggidyj said in QStyledItemDelegate doesn't move with its QTreeView column:
In my QAbstractItemModel, I insert headers to the front of a list (basically self._headers.insert(0,headerdata) each time).
In my QTreeView, I connect self.model().columnsInserted.connect(self._assignDelegates). In _assignDelegates(), I loop over the range of logical columns provided by the signal, and based on whatever the particular column is, I call self.setItemDelegateForColumn(col, delegate). (In my troubleshooting I've also tried converting the column index to visual via self.header().visualIndex(col), but this doesn't fix it.)
This would be a lot easier to follow with a minimal working example, with emphasis on both minimal, and working to demonstrate the issue.
So at this point, the first column of my QTreeView has the delegate assigned. But as soon as the next column is inserted, I would expect the existing delegate to shift down with its existing column. But instead, it seems that it "sticks" with visual column 0. So I'm essentially just always swapping out the delegate on visual column 0 over and over.
But then also, if I drag around a column header to visually rearrange the column order in the QTreeView, the delegate follows correctly. So, geez, pick a behavior.
I don't see an inconsistency. When a delegate for a column is assigned, the column number refers to the column returned by QModelIndex::column() at the time the delegate is drawn. Inserting a column before a particular item means that the index for that item will have a new column number. The model has been altered, and delegates are used by the view to render the model as it is, not as it was in the past.
Dragging the columns in a view around will not result in a new column, because the underlying model hasn't changed.
What's the best way of solving this? Best I can think of is re-applying all delegates to every column every time any columns are inserted or moved in the model. But that's ridiculously inefficient IMO.
I don't understand what this is. My first thought is to suggest one or more of:
a proxy model to maintain the logical column concept
restructuring the model population
waiting for model population to complete before assignment of delegates, or even the view
Perhaps code, images, or a reference to software implementing the desired interface will help.