PDA

View Full Version : removeRows in a QTreeView



mhoover
31st August 2009, 21:05
I am using the basic QStandardItemModel to populate a QTreeView.

I have looked over all the removeRow/removeRows issues I could find on this forum, but they were not able to solve my problem.

I have been retrieving indexes to append and edit items in the QTreeView, but for some reason when I call removeRow on the this index I get bizarre behavior. I typically see nothing happen but occasionally a child node will disappear.

I have tried removing the nodes using this approach:


QModelIndex index = ui.settings_treeView->currentIndex();
QStandardItem *item = settings_model->itemFromIndex( index );
int row = settings_model->itemFromIndex( index )=>row();
settings_model->removeRow( row, index );

And I have tried this approach:


QItemSelectionModel *selection = ui.settings_treeView->selectionModel();
QModelIndex index = ui.settings_treeView->currentIndex();
settings_model->removeRows( selection->currentIndex().row(), 1, index );

But random nodes start disappearing.

This seems like something that should be easy. I'm not sure why it isn't working for me.

wysota
31st August 2009, 21:32
You want to remove the currently highlighted item?


QTreeView *tv = ...;
QModelIndex current = tv->currentIndex();
if(current.isValid()){
tv->model()->removeRow(current.row(), current.parent());
}

mhoover
1st September 2009, 00:37
Thanks.

I wasn't sure about that parent thing. It didn't look easy to find the parent, but I guess it comes with the index.

Also the validity check is nice to know about.