0
votes

I'm a Python/PyQt (v4) newbie who is subclassing the QListWidget and making use of its internal drag and drop mechanism:

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class AlgorithmListControl(QListWidget):
    def __init__(self, parent = None):
        super(AlgorithmListControl, self).__init__(parent)

        self.setAcceptDrops(True)
        self.setDragEnabled(True)
        self.setDragDropMode(QAbstractItemView.InternalMove)

I'm happy with the default behavior and appearance and I'm able to drag/drop items internally in the widget. Now, I'd like to add some custom logic that occurs when the drag operation begins and after the drop operation ends. The trouble I'm having is if I handle the events, for example the dragEnterEvent as follows:

def dragEnterEvent(self, event):
    ...

then I no longer get the nice default drag/drop behavior (I'm unable to drag an item to a new location, I don't get the horizontal insertion indicator, etc). How do I retain the standard drag-drop behavior but add my custom logic at these two event points? Thank you in advance.

1

1 Answers

1
votes

Since you override the method when you use the same name, you need to implement all the actions that happen in the standard way, or call the parent (standard) method inside.

You can call the method from the parent class with inheritance, thanks to the function super()and then implement your own logic :

def dragEnterEvent(self, event):
    super().dragEnterEvent(event)
    print('enter')