[PyQt] Drag-and-drop editing in QListWidget or QListView

David Boddie david at boddie.org.uk
Fri Jun 3 00:42:45 BST 2011

On Thu Jun 2 14:05:41 BST 2011, Nathan Weston wrote:

> I have a QListWidget with dragDropMode set to InternalMove, and I can
> move rows around by dragging as expected. But I also want to know when
> items are moved so I can update other data in my application, and I
> can't find any way to do this.

I don't think there's any way to get notified of moves when InternalMove
is used.

> Would it help if I switched to QListView and implemented a model? I
> looked into this a bit but couldn't figure out how to do what I want.

You can stay with a QListWidget if you want, but the mechanisms used to
handle dragging and dropping will be a bit different to those you would
use with QListView (where you would delegate most of this to the model).

The draganddrop/puzzle example provided with PyQt uses a QListWidget to
display a collection of puzzle pieces that can be dropped inside or outside
the list, though it's not exactly the most user friendly interface you
could imagine.

The itemviews/puzzle example is more or less the same, but uses QListView
instead of QListWidget. It implements support for drag and drop in a
custom model, which is based on QAbstractListModel to make it as simple as
possible. Strangely, the piece movement is quite a bit more intuitive than
in the other example.


More information about the PyQt mailing list