[PyQt] Using drag+drop in a QComboBox

Andreas Pakulat apaku at gmx.de
Tue Oct 30 22:52:27 GMT 2012


Hi,

On Tue, Oct 30, 2012 at 7:54 PM, Chris Beaumont <beaumont at hawaii.edu> wrote:
> Fair enough. My application is a window that displays one of several images
> for scientific image analysis. A combo box selects which image is displayed.
> It's a nice feature that you can "blink" back and forth between two images
> with adjacent entries in the combo box by hitting up/down while the box has
> keyboard focus. If the items were movable, the user could set up this
> blinking feature for any two images, even if they were not initially
> adjacent in the combo box.

Hmm, I see. I'd personally favor a list with a selected item in it,
but apparently thats not the real problem in this case anyway...

> However, even if I chose a more "traditional" drag+drop widget (say a
> QListView), the same thing happens (see code below). The problem seems to be
> that you can drop items both on top of existing rows or between rows.
> Dropping on top of rows deletes the dragged item. Is this the correct
> behavior? Is there a way to restrict drops on list views to be between items
> only?

I think the problem in this case is that you use a model on a listview
which is able to support tree's. Without looking at the code, I think
the standard item models behaviour when dropping an item onto another
is to add the dropped item to the list of children of the item it was
dropped on. You can see this when using a QTreeView.

So change to a model which only supports lists and has the behaviour
outlined in the model/view docs at
http://qt-project.org/doc/qt-4.8/model-view-programming.html#inserting-dropped-data-into-a-model
(the part about using the parent index row to insert into the top
list) as the standard item model does not seem to allow changing this
behaviour - except by overriding dropMimeData.

Andreas


More information about the PyQt mailing list