Raise and lower QTreeWidgetItem in QTreeWidget?

The question says everything how you raise and lower [change positions] QTreeWidgetItems in QtreeWidget,

+4
source share
2 answers

I believe that you will need to use the model object to be able to manipulate the positions of the positions (if that's what you want to do). See an example below; he moves the first element of the abstract model to the bottom.

QAbstractItemModel* model = your_tree_view->model(); QModelIndex index0 = model->index(0, 0); QMap<int, QVariant> data = model->itemData(index0); // check siblings of the item; should be restored later model->removeRow(0); int rowCount = model->rowCount(); model->insertRow(rowCount); QModelIndex index1 = model->index(rowCount, 0); model->setItemData(index1, data); 

once an element is moved in the model, your vision widget should update itself accordingly

if you need to resize the element displayed by your tree, set the delegate element and override its sizeHint method

hope this helps, believes

+4
source

I found the serge solution rather complicated, but for simple up / down movement. This is actually a pretty simple solution since you are using QTreeWidget:

 QTreeWidgetItem* item = your_qtreewidget->currentItem(); int row = your_qtreewidget->currentIndex().row(); if (item && row > 0) { your_qtreewidget->takeTopLevelItem(row); your_qtreewidget->insertTopLevelItem(row - 1, item); your_qtreewidget->setCurrentItem(item); } 

Here you have the code to move the item up. From this you can find how to quickly move it :)!

+4
source

All Articles