[PyQt] how to close editor in QTreeView

Hans-Peter Jansen hpj at urpla.net
Fri Oct 8 19:38:13 BST 2010


On Friday 08 October 2010, 18:55:11 Danny wrote:
> Howdy,
>
> In my application, I create a tree by drag and drop. When I drop an
> item, I immediately open the item editor delegate so the user can
> type the item text. I am implementing undo/redo, and both the item
> creation and text editing go into a macro. All is good.
>
> The problem is that if I have the editor open and I begin another
> item drop. The drop happens before the editor closes and my macro
> gets messed up. This is all a long winded way of justifying that I
> need to programmatically close an open item delegate if there is one
> at the beginning of the drop operation.
>
> I can't get this to work. I have tried the following logic:
>
> index = myTreeView.currentIndex()
  ^^^^^
  currentIndex, I guess
> delegate=myTreeView.itemDelegate(currentIndex)
> myTreeView.closeEditor(delegate)
>
> This fails with an error that closeEditor expects a
> QAbstractItemDelegate and a QStyledItemDelegate.

Either try using QAbstractItemView.closePersistentEditor() or, if you 
need custom editors, create your own with a QStyledItemDelegate 
subclass, where you have full control (including the widget, that 
you're missing).

Pete


More information about the PyQt mailing list