Results 1 to 8 of 8

Thread: PyQt: zooming with QGraphicsSceneWheelEvent

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2017
    Posts
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question PyQt: zooming with QGraphicsSceneWheelEvent

    I am new to both (Py)Qt and Python. I am currently trying to write my first PyQt app in the form of a simple image viewer I have dubbed shufti. So far I have got it to load images files from the command line and... well that's about all so far. Now I want to be able to zoom in and out of the shufti QGraphicsView window by scrolling the mousewheel over the window but I'm having trouble figuring out how that should work.

    What I have been able to deduce is that I should be able to achieve this using QGraphicsSceneWheelEvent / QGraphicsSceneWheelEvent.delta and then I'd need to hook that up to adjust the self.view.scale() object within the initUI method, somehow.

    I've had a search but I've not been able to find any examples of people using QGraphicsSceneWheelEvent / WheelEvent with PyQt so I'm hoping someone could explain how I might get the two interacting?

    Thanks


    Qt Code:
    1. #!/usr/bin/python3
    2. # -*- coding: utf-8 -*-
    3. #
    4. # shufti - A WIP, PyQt5 persistent image viewer
    5. #
    6. # By Dan MacDonald
    7. #
    8. # 2017
    9. #
    10. # Usage:
    11. #
    12. # python shufti.py path/to/image
    13.  
    14. import sys
    15. from PyQt5 import QtCore
    16. from PyQt5.QtGui import QPixmap
    17. from PyQt5.QtWidgets import QMainWindow, QApplication, QGraphicsScene, QGraphicsView
    18.  
    19. class Shufti(QMainWindow):
    20.  
    21. def __init__(self):
    22. super().__init__()
    23. try:
    24. self.file = open(sys.argv[1], 'r')
    25. except IOError:
    26. print('There was an error opening the file')
    27. sys.exit(1)
    28.  
    29. if (sys.argv[1]).lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.pbm', '.pgm', '.ppm', '.xbm', '.xpm')):
    30. self.initUI()
    31. self.setWindowTitle("shufti")
    32. self.resize(self.img.size())
    33. else:
    34. print("Unsupported file format")
    35. sys.exit(1)
    36.  
    37. def initUI(self):
    38.  
    39. self.img = QPixmap(sys.argv[1])
    40. self.scene = QGraphicsScene()
    41. self.scene.addPixmap(self.img)
    42. self.view = QGraphicsView(self.scene, self)
    43. self.view.resize(self.img.width() + 2, self.img.height() + 2)
    44. self.show()
    45.  
    46. def toggleFullscreen(self):
    47. if self.isFullScreen():
    48. self.showNormal()
    49. else:
    50. self.showFullScreen()
    51.  
    52. def keyPressEvent(self, event):
    53. if event.key() == QtCore.Qt.Key_F11 or event.key() == QtCore.Qt.Key_F:
    54. self.toggleFullscreen()
    55.  
    56.  
    57. if __name__ == '__main__':
    58.  
    59. app = QApplication(sys.argv)
    60. shufti = Shufti()
    61. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    Last edited by danboid; 12th February 2017 at 18:39.

Similar Threads

  1. Replies: 0
    Last Post: 29th January 2015, 10:55
  2. Upgrading from PyQt 4.5 to PyQt 4.7
    By RogerCon in forum Installation and Deployment
    Replies: 0
    Last Post: 14th July 2010, 18:52
  3. zooming only x
    By mastupristi in forum Qwt
    Replies: 1
    Last Post: 7th May 2009, 08:23
  4. Zooming..
    By chethana in forum Qt Programming
    Replies: 7
    Last Post: 10th October 2007, 08:17
  5. Zooming in but not out
    By nitriles in forum Qt Programming
    Replies: 1
    Last Post: 5th October 2007, 08:54

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.