QTreeWidget: signal emission on internal moves of children within a set of parents
Initial setup
For this problem we are given QTreeWidget
which consists of set of parents which can have a set of children. For simplicity we set the children to behave like leafs which means they cannot have their own children. The figure below provides an example of such tree structure:
Assume, we have implemented the rules for drag and drop of the children, for example, we can only drag one child at a time to another parent; and we cannot drag the parent at all.
Problem statement
For the given setup we want to be able to emit signal whenever the drag and drop action occurs. By default Qt does not provide such a signal. While drag and drop is carried out by Qt within the QTreeWidget
, the signal functionality might be useful if we also want to work with certain external data.
As an example, in my case it was OpenSceneGraph data structure and my task was to perform scene graph re-arrangement whenever drag-and-drop occurred.
The result output parameters should contain the next information:
- parent top-level item from where the children are moved
- start and end indexes of child(-ren) before the move; within the parent item
- destination top-level item to where the children are moved
- row index where the children were inserted within the destination item
Solution
In order to obtain and pass the required parameters on drag and drop within QTreeWidget
, we have to re-implements its protected method dropEvent
. Here is the code sample of how to do it:
If necessary we can always obtain the indices of both parent
and destination
by using QTreeWidget::indexOfTopLevelItem(QTreeWidgetItem*)
.
The sample above provides an example when only one child can be dragged. In case for the multiple children drag (consequitive order), we would have to assign end = this->indexFromItem(kids.at(kids.size()-1)).row();
.
Leave a Comment