38791

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 drop events. But while testing this out, i've noticed that each event is emmitted twice.

Here's my MooView code:

from PyQt4 import QtCore, QtGui class MooView(QtGui.QGraphicsView): def __init__(self, parent = None): QtGui.QGraphicsView.__init__(self, parent) self.handlers = {} self.handlers['dragEnter'] = [] self.handlers['dragEnter'].append(super(MooView, self).dragEnterEvent) self.handlers['dragLeave'] = [] self.handlers['dragLeave'].append(super(MooView, self).dragLeaveEvent) self.handlers['dragMove'] = [] self.handlers['dragMove'].append(super(MooView, self).dragMoveEvent) self.handlers['drop'] = [] self.handlers['drop'].append(super(MooView, self).dropEvent) def addDragEnterHandler(self, handler): self.handlers['dragEnter'].append(handler) def removeDragEnterHandler(self, handler): self.handlers['dragEnter'].remove(handler) def addDragLeaveHandler(self, handler): self.handlers['dragLeave'].append(handler) def removeDragLeaveHandler(self, handler): self.handlers['dragLeave'].remove(handler) def addDragMoveHandler(self, handler): self.handlers['dragMove'].append(handler) def removeDragMoveHandler(self, handler): self.handlers['dragMove'].remove(handler) def addDropHandler(self, handler): self.handlers['drop'].append(handler) def removeDropHandler(self, handler): self.handlers['drop'].remove(handler) # handlers def dragEnterEvent(self, arg): #res = super(MooView, self).dragEnterEvent(arg) for h in self.handlers['dragEnter']: h(arg) #return res def dragLeaveEvent(self, arg): #res = super(MooView, self).dragLeaveEvent(arg) for h in self.handlers['dragLeave']: h(arg) #return res def dragMoveEvent(self, arg): #res = super(MooView, self).dragMoveEvent(arg) for h in self.handlers['dragMove']: h(arg) #return res def dropEvent(self, arg): #res = super(MooView, self).dropEvent(arg) for h in self.handlers['drop']: h(arg) #return res

And this is how i run that core:

import sys from PyQt4 import QtCore, QtGui, QtOpenGL from window import Ui_MainWindow 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!') self.scene = QtGui.QGraphicsScene() self.ui.workspace_view.setScene(self.scene) rect = QtCore.QRectF(0, 0, 1000, 1000) self.scene.setSceneRect(0, 0, rect.width(), rect.height()) self.ui.workspace_view.setViewport(QtOpenGL.QGLWidget()) 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) self.ui.workspace_view.addDragEnterHandler(self.workspace_item_drag_enter) self.ui.workspace_view.addDragMoveHandler(self.workspace_item_drag_move) self.ui.workspace_view.addDropHandler(self.workspace_item_dropped) def workspace_item_drag_move(self, e): #print("item drag move") e.accept() def workspace_item_drag_enter(self, e): print("item drag enter") e.accept() def workspace_item_dropped(self, e): print("item dropped") if (__name__ == "__main__"): app = QtGui.QApplication(sys.argv) myApp = Main() myApp.show() sys.exit(app.exec_())

When drag-n-dropping items, "item drag enter" and "item dropped" lines are printed twice each in the terminal, e.g.:

item drag enter item drag enter item dropped item dropped

<strong>The question is:</strong> how it could be fixed???

Answer1:

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 class MooGraphicsScene(QtGui.QGraphicsScene): def __init__(self, parent = None): QtGui.QGraphicsScene.__init__(self, parent) def dragMoveEvent(selfs, e): pass def dropEvent(self, e): print("Drop fired!") def dragEnterEvent(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!

Recommend

  • Eclipse Files Not Found? Works with JCreator
  • Issue with fitInView of QGraphicsView when ItemIgnoresTransformations is on
  • Pygame error: mixer system not initialized
  • Having a relative path in a module relative to the file calling the module
  • How To Pass a MultiDimensional Array from Javascript to server using PageMethods in ASP.Net
  • How to send None with Signals across threads?
  • Can I convert bool in model to yes/no in a QComboBox
  • How to handle Back Button to remove current fragment In MvvmCross
  • NSMutableArray Access Issue
  • Typecasting `this` of a base class template to its derived class
  • How to eliminate warning for passing multidimensional array as const multidimensional array?
  • Jquery UI Sortable, move item automatically
  • How do you remove the JComboBox 'click and see dropdown' functionality?
  • How to calculate networkdays minus holidays between 2 dates
  • Efficient algorithm to find additions and removals from 2 collections
  • XOR with Neural Networks (Matlab)
  • How to resolve permission denied maybe missing internet permission?
  • Detecting # in Scheme list
  • Correct implementation of List Iterator methods
  • Doctrine2 bulk import try to work with another entity
  • How can I tell a form not to dispose a particular control when it closes?
  • SyntaxError: (irb):26: both block arg and actual block given
  • std::remove_copy_if_ valgrind bytes in block are possibly lost in loss record
  • CakePHP ACL tutorial initDB function warnings
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • Code in Job's Script Block after Start-Process Does not Execute
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • SetUp method failed while running tests from teamcity
  • How to disable jQuery.jplayer autoplay?
  • Understanding cpu registers
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?