PDA

View Full Version : pyqt qgraphicsView: Get coordinate from image



mshemuni
27th February 2013, 00:26
Hi everyone.
There is a qgraphicsView and I made zooming enable with scroll ball. Now I want get coordinates when I click on it. However on zooming coordinates should change and I want it to return coordinates from image. Is it possible? Here my code:



def display(self, FilePath, displayDevice):
if os.path.isfile(FilePath):
scene = QtGui.QGraphicsScene()
scene.addPixmap(QtGui.QPixmap(FilePath))
displayDevice.setScene(scene)




def zoom(self, display, val):
display.scale(val, val)

lanz
27th February 2013, 08:00
You can take click coordinates on the view, and then use display.mapToScene to get scene coordinates, I think it should be aware of scaling/rotating etc.

mshemuni
27th February 2013, 13:10
Have you any example?
I'm a newbie.

lanz
27th February 2013, 13:26
Well, I'll try, but since I'm not very familiar with pyqt, expect it to be somewhat flaky :D
You'll need to overload mouse event handler, so you'll need custom graphics view

class CustomGraphicsView(QGraphicsView):
def mousePressEvent(self, mouse_evt):
point = self.mapToScene (mouse_evt.pos)
And in point you'll get point in scene coords. Of course you should declare your view as CustomGraphicsView.