[PyQt] GraphicsItem, QObject Inheritance problem

Brian Kelley kelley at eyesopen.com
Thu Oct 1 15:34:59 BST 2009


All you need to connect signals and slots is a qobject.  You can create any qobject for this task.

Here is a concrete example, actually using a QGraphicsItem.  This makes an Image button that emits a signal "clicked()" and move the graphics to mimic a button press:

class ImageButton(QtGui.QGraphicsPixmapItem):
    def __init__(self, pixmap, parent=None, oneshot=True):
        QtGui.QGraphicsPixmapItem.__init__( self, pixmap, parent )
        self.emitter = QtCore.QObject()
        self.emitter.setObjectName("ImageButtonEmitter")
        self.oneshot = oneshot
        self.callback = None

    def mousePressEvent(self, event):
          self.moveBy(1,1)
          self.emitter.emit( QtCore.SIGNAL("clicked()") )
          if self.callback:
              self.callback()

    def mouseReleaseEvent(self, event):
        if not self.oneshot:
          self.moveBy(-1,-1)

To connect to this signal as follows:

foo = ImageButton(...)
foo.emitter.connect( foo.emitter, QtCore.SIGNAL("clicked()"), pythonfunc )

I find this to be cleaner than wrapping a QObject around the graphics item, but in internet speak YMMV.

Brian

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20091001/34c63f92/attachment-0001.html


More information about the PyQt mailing list