2

I'm trying to link a mouse press event to displaying the coordinates of the mouse when clicked in a QLabel. A few problems... when I was passing a generic QWidget.mousePressEvent, the coordinates would only display the first time clicked. When I tried to make the mouse event specific to a GraphicsScene(self.p1), I get the following error:

Traceback (most recent call last):
  File "C:\Users\Tory\Desktop\DIDSONGUIDONOTCHANGE.py", line 59, in mousePressEvent
    self.p1.mousePressEvent(event)
TypeError: QGraphicsWidget.mousePressEvent(QGraphicsSceneMouseEvent): argument 1 has unexpected type 'QMouseEvent'

This is the code I'm using... I know it's off but I'm new to this and a bit lost at where to begin.

def mousePressEvent(self, event):
    self.p1.mousePressEvent(event)
    x=event.x()
    y=event.y()
    if event.button()==Qt.LeftButton:
        self.label.setText("x=%0.01f,y=%0.01f" %(x,y))

How do I get the mouse click to display the coordinates of the graphics scene self.p1?

Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
Victoria Price
  • 637
  • 3
  • 13
  • 26
  • `QGraphicsSceneMouseEvent` and `QMouseEvent` are different classes. Most (all?) of `QGraphicsScene` events are different from `QWidget` events. What are you trying to achieve with the line: `self.p1.mousePressEvent(event)`? – Avaris Aug 24 '12 at 20:39
  • I guess that's where I'm getting stuck-- I'm not sure where its getting the QMouseEvent from, when I'm specifically trying to ask for a QGraphicsSceneMouseEvent (p1 is a QGraphicsScene object). I have data displayed as an image in p1 and it has its own coordinate system scaled to that image's real world dimensions, so I'm trying to get the coordinates of a mouse click relative to those dimensions. – Victoria Price Aug 24 '12 at 20:45
  • You are not asking anything from `p1`. You are sending it an `event`, which happens to be a `QMouseEvent`. Why? You are not showing whose `mousePressEvent` you are overriding, but I'm guessing it is a `QWidget` derivative. – Avaris Aug 24 '12 at 20:52

1 Answers1

2

It looks like an event filter may do what you want.

Here's a simple demo:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.scene = QtGui.QGraphicsScene(self)
        self.scene.addPixmap(QtGui.QPixmap('image.jpg'))
        self.scene.installEventFilter(self)
        self.view = QtGui.QGraphicsView(self)
        self.view.setScene(self.scene)
        self.label = QtGui.QLabel(self)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.view)
        layout.addWidget(self.label)

    def eventFilter(self, source, event):
        if (source is self.scene and
            event.type() == QtCore.QEvent.GraphicsSceneMouseRelease and
            event.button() == QtCore.Qt.LeftButton):
            pos = event.scenePos()
            self.label.setText('x=%0.01f,y=%0.01f' % (pos.x(), pos.y()))
        return QtGui.QWidget.eventFilter(self, source, event)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Ah, that's perfect! Thank you, worked beautifully. I'm quite new to PyQt (and Python in general) so thanks for bearing with me being a bit unclear! – Victoria Price Aug 27 '12 at 11:22