0
votes

I have a QTreeWidget, which I noticed has the functionality to drag the column headers from their original location to somewhere new. I want to use this feature in my application, but when I drag a column header to a new location, the QTreeWidget does not seem to retain the column changes in any of the QTreeWidgetItems.

I tried to loop through the columns and print out the text of each column in the QTreeWidget's header item after a header was dragged to a new location, but this did not seem to retain any of the changes. Next, I tried overriding the QTreeWidget::dropEvent function, but I noticed that this function is only activated when a QTreeWidgetItem is dragged to a new location, not a column.

Debug code to print out the text of each column in the HeaderItem:

column_count = self.ui.treeWidget.headerItem().columnCount()
for ii in range(column_count):
    print(self.ui.treeWidget.headerItem().text(ii))

I don't need to catch the change right when it happens, I just want to be able to find whatever changes were applied to the QTreeWidget columns at some point in time after one of these column drag and drops. I would expect the debug code above to print out the text of the newly ordered headerItem columns, but it still prints the header columns in the old order before any columns were dragged to a new location.

1
I'm not sure I really understand the question but... are you perhaps looking for the visual index <--> logical index column mapping? If so then have a look at QHeaderView::logicalIndex and QHeaderView::visualIndex.G.M.
Thank you! Instead of only directly accessing the default header with QTreeWidget::headerItem, I first created a QHeaderView object, set its parent to be my tree widget, and then used QHeaderView::logicalIndex to get the headers with their new index after they were modified by the user moving them. I will update the question with an answer and the modified debug code.jkulskis

1 Answers

0
votes

@G.M. helped point me in the right direction in the comments to get around this problem. Originally, I was directly accessing my header with the QTreeWidget::headerItem method, but to get the logical index I needed to set the header to be a QHeaderView object.

I created a QHeaderView, enabled its movable sections functionality, and set it as the header for my QTreeWidget:

self.headerView = QtWidgets.QHeaderView(QtCore.Qt.Orientation.Horizontal)
self.headerView.setSectionsMovable(True)
self.ui.treeWidget.setHeader(self.headerView)

Then, when I wanted to loop through the columns after a change has been made, I found the text of each column using the column index returned by QHeaderView::logicalIndex.

The modified debug code to loop through the columns and print out the text of the column sections after they are moved by the user is below:

column_count = self.headerView.count()
for ii in range(column_count):
    logical_index = self.headerView.logicalIndex(ii)
    print(self.ui.treeWidget.headerItem().text(logical_index))