PDA

View Full Version : Moving QTreeWidgetItem Up and Down in a QTreeWidget



Staakman
16th September 2013, 14:36
Hi,

I want to be able to swap two QTreeWidgetItem positions in a QtreeWidget.
With other words move items up and down 1 position in a QtreeWidget.

9597

After searching for hours online I just can't find a proper solution.
Not a single one of them works.
Have to add to this that I'm very new to QT.

Does anyone have a solution to my problem?

Thanks in advance.

anda_skoa
16th September 2013, 20:37
You get the parent item, then you call its indexOfChild method to determine the index if your current item, then call takeItem() and then insertItem with either index - 1 or index + 1

Cheers,
_

Staakman
17th September 2013, 11:34
Worked as a charm!
Thank you very much.



QTreeWidgetItem* item = this->currentItem();
int row = this->currentIndex().row();
if (item && row > 0)
{
QTreeWidgetItem* parent = item->parent();
int index = parent->indexOfChild(item);
QTreeWidgetItem* child = parent->takeChild(index);
parent->insertChild(index-1, child);
parent->setExpanded(true);
child->setExpanded(true);
}