Skip to content Skip to sidebar Skip to footer

Pyqt Event Emmitted Twice

I'm trying to implement drag-n-drop'in' items from QListWidget to QGraphicsView. I've inherited my own class MooView from QGraphicsView and encapsulated dragMove, dragEnter and dro

Solution 1:

As i've read somewhere in the web, QGraphicsView could pass events to its QGraphicsScene in some cases. So it did here: when i inherited my class from QGraphicsScene and used it as default for QGraphicsView (replaced MooView with QGraphicsView; read the rest of this answer for details) it worked as i was expecting.

So, now i have two source parts: MooGraphicsScene.py and main.py. Here are they, respectively:

from PyQt4 import QtCore, QtGui

classMooGraphicsScene(QtGui.QGraphicsScene):
    def__init__(self, parent = None):
        QtGui.QGraphicsScene.__init__(self, parent)

    defdragMoveEvent(selfs, e):
        passdefdropEvent(self, e):
        print("Drop fired!")

    defdragEnterEvent(self, e):
        e.accept()
        print("Drag entered!")

And this one:

import sys

from PyQt4 import QtCore, QtGui, QtOpenGL
from window import Ui_MainWindow
from MooGraphicsScene import MooGraphicsScene

class Main(QtGui.QMainWindow):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.setWindowTitle('Hello, Qt!')

        # Setup Workspace
        self.scene = MooGraphicsScene()
        self.ui.workspace_view.setScene(self.scene)

        rect = QtCore.QRectF(0, 0, 1000, 1000)
        self.scene.setSceneRect(0, 0, rect.width(), rect.height())

        icon = QtGui.QIcon(QtGui.QPixmap(":/Images/50.png"))
        text = "50's element"
        item = QtGui.QListWidgetItem(icon, text)
        self.ui.element_list.addItem(item)

        icon = QtGui.QIcon(QtGui.QPixmap(":/Images/40.png"))
        text = "40's element"
        item = QtGui.QListWidgetItem(icon, text)
        self.ui.element_list.addItem(item)

if (__name__ == "__main__"):
    app = QtGui.QApplication(sys.argv)
    myApp = Main()
    myApp.show()
    sys.exit(app.exec_())

Hope, this would help someone!

Post a Comment for "Pyqt Event Emmitted Twice"