P
P
Philip Bondarev2017-08-30 18:59:21
Python
Philip Bondarev, 2017-08-30 18:59:21

Right click image panning in PyQt5?

There is an application, it has an overridden QGraphicsView, and an image in it. It is necessary to click the left mouse button on the image and call the pixelSelect function, which determines the color of the clicked pixel and its location (In the example below, not all the code). When you hold down the right button, you must enable the QGraphicsView.ScrollHandDrag mode to pan. I understand that the algorithm should be something like this. At initialization - mode:

self.setDragMode(QGraphicsView.ScrollHandDrag)
self.mousePressEvent = self.pixelSelect

Then, we catch the right-click and switch to the mode:
self.setDragMode(QGraphicsView.ScrollHandDrag)
I try something like this, but I go into recursion due to self._photo.mousePressEvent(event):
def set_pixmap(self, msg, w_img, h_img):
    self._zoom = 0
    self.qImg = QImage(msg, w_img, h_img, w_img, QImage.Format_Grayscale8)
    self._photo = QGraphicsPixmapItem(QPixmap(self.qImg))
    self._photo.mousePressEvent = self.pixelSelect
    self._scene.addItem(self._photo)

def pixelSelect(self, event):
    if event.button() == 1:
        self.setDragMode(QGraphicsView.NoDrag)
        position = QPoint(event.pos().x(), event.pos().y())
        print position
    else:
        self.setDragMode(QGraphicsView.ScrollHandDrag)
        self._photo.mousePressEvent(event)

Help me please.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question