Search code examples
c++pyqtpysideqtreeview

How to remove widget from QAbstractItemView


QAbstractItemView has `setIndexWidget but doesn't have "remove" function.

I tried to work it around by storing widgets that are passed to QAbstractItemView and hide / delete them when I want to remove them from the view, which ends up with RuntimeError: wrapped C/C++ object of %S has been deleted error. I assume that is due to the ownership of the widgets are passed to viewport, as the doc says:

Sets the given widget on the item at the given index, passing the ownership of the widget to the viewport.

How can I remove widgets?

I'm using PyQt but I assume it's valid question for C++ and PySide too.


Solution

  • Found a workarount that works for me. Using QAbstractItemModel::reset() seems to remove widgets that are set by setIndexWidget() too.

    It's not very intuitive to me since I'm setting widgets to QAbstractView not ItemModel.

    More precisely, API doc recommends to use beginResetModel() and endResetModel() instead of reset() which I follow.