Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Zoom in on the current mouse position of a QGraphicsView without losing quality
QtWS25 Last Chance

Zoom in on the current mouse position of a QGraphicsView without losing quality

Scheduled Pinned Locked Moved Unsolved General and Desktop
pyqt5python3qgraphicsviewqpixmap
6 Posts 2 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    emiliathewhite
    wrote on 23 Apr 2022, 02:34 last edited by
    #1

    I know this is possible by using the scale method of the QGraphicsView and setting the anchor using setTransformationAnchor(QGraphicsView.AnchorUnderMouse). However, doing so would result to the image quality being compromised when zooming.

    So what I did instead is to scale the pixmap, and set it as the QGraphicsPixmapItem of the scene. To zoom in on the current mouse position, I use the mapToScene method and position of the wheel event, but this is still not as smooth as the "traditional" implementation. Any idea how I can improve this behavior?

    Code:

    class Zoom_View(QGraphicsView):
    
        def __init__(self):
            super().__init__()
    
            self.scene = QGraphicsScene()
            self.setScene(self.scene)
            self.pix_map = QPixmap("path/to/file")
            self.pix_map_item = self.scene.addPixmap(self.pix_map)
            self.global_factor = 1
    
        def scaleImage(self, factor):
            _pixmap = self.pix_map.scaledToHeight(int(factor*self.viewport().geometry().height()), Qt.SmoothTransformation)
            self.pix_map_item.setPixmap(_pixmap)
            self.scene.setSceneRect(QRectF(_pixmap.rect()))
    
        def wheelEvent(self, event):
            factor = 1.5
    
            if QApplication.keyboardModifiers() == Qt.ControlModifier:
                view_pos = event.pos()
                scene_pos = self.mapToScene(view_pos)
                self.centerOn(scene_pos)
    
                if event.angleDelta().y() > 0 and self.global_factor < 20:
                    self.global_factor *= factor
                    self.scaleImage(self.global_factor)
                elif event.angleDelta().y() < 0 and self.global_factor > 0.2:
                    self.global_factor /= factor
                    self.scaleImage(self.global_factor)
            else:
                return super().wheelEvent(event)
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 Apr 2022, 19:07 last edited by
      #2

      Hi and welcome to devnet,

      What about scaling the view rather than the pixmap ?

      See this wiki article for a possible implementation.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        emiliathewhite
        wrote on 24 Apr 2022, 08:54 last edited by
        #3

        @SGaist said in Zoom in on the current mouse position of a QGraphicsView without losing quality:

        What about scaling the view rather than the pixmap ?

        Hello @SGaist do you mean using QGraphicsView scale method? I have already tried that and it zooms in properly on the current mouse position when I set the anchor to AnchorUnderMouse. However the quality of the image is awful since the scale method has no smooth transformation mode

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 24 Apr 2022, 19:13 last edited by
          #4

          By the way, what is the resolution of your original image ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          E 1 Reply Last reply 25 Apr 2022, 05:10
          0
          • S SGaist
            24 Apr 2022, 19:13

            By the way, what is the resolution of your original image ?

            E Offline
            E Offline
            emiliathewhite
            wrote on 25 Apr 2022, 05:10 last edited by
            #5

            @SGaist 300 dpi. Even at high resolution, the image will be pixelated if I use the scale method

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 26 Apr 2022, 10:56 last edited by
              #6

              Depending on your end goal, you might want to consider using a library like OpenCV to do the zooming part and Qt to display the result.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              1/6

              23 Apr 2022, 02:34

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved