开发者

Raise and lower QTreeWidgetItem in a QTreeWidget?

开发者 https://www.devze.com 2022-12-16 02:28 出处:网络
Question says it all how do you 开发者_如何学JAVAraise and lower [change the positions of ] QTreeWidgetItems in a QtreeWidget ,I believe you would need to use model object to be able to manipulate ite

Question says it all how do you 开发者_如何学JAVAraise and lower [change the positions of ] QTreeWidgetItems in a QtreeWidget ,


I believe you would need to use model object to be able to manipulate items positions (if this what you want to do). Please check an example below; it moves the first item 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 item moved in the model your treeview widget should update itself accordingly

in case you need to change the size of the item shown by your treeview install an item delegate and override its sizeHint method

hope this helps, regards


I found serge's solution to be quite complicated as well for a simple move up / move down. There's actually a quite easier solution to this issue since you're 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 a code to move an item up. From this you should be able to find how to move it down in no time :) !

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号