{"id":554,"date":"2012-11-15T15:10:22","date_gmt":"2012-11-15T21:10:22","guid":{"rendered":"http:\/\/www.gergltd.com\/home\/?p=554"},"modified":"2012-11-28T14:36:11","modified_gmt":"2012-11-28T20:36:11","slug":"getting-qgraphicsview-with-a-pixmap-to-zoom-and-scroll-like-the-windows-photo-viewer","status":"publish","type":"post","link":"https:\/\/www.gergltd.com\/home\/getting-qgraphicsview-with-a-pixmap-to-zoom-and-scroll-like-the-windows-photo-viewer\/","title":{"rendered":"Getting QGraphicsView with a pixmap to Zoom and Scroll Like the Window&#8217;s Photo Viewer"},"content":{"rendered":"<p>This took me forever to figure out. \u00a0I noticed lots of people online had the same question but few got it working properly and some had to do a hack. \u00a0Here is the clean way to do this. \u00a0The trick is in setTransformationAnchor() and setDragMode()<\/p>\n<p>My setup code in my QDailogs __init__ function:<\/p>\n<pre>imageFilename = QFileDialog.getOpenFileName(self,'select image')\r\nself._pixMap = QPixmap.fromImage(QImage(imageFilename))\u00a0\r\nself._graphicsScene = QGraphicsScene(self)\r\nself._graphicsScene.addPixmap(self._pixMap)\r\nself._view = MyGraphicsView()\r\nself._view.setScene(self._graphicsScene)\r\n\r\n# Create a vertical layout.\r\nself._layout = QVBoxLayout()\r\nself._layout.setMargin(0)\r\nself._layout.setSpacing(0)\r\n\r\n# Add label widget\r\nself._layout.addWidget(self._view)\r\n# set widget to layout\r\nself.setLayout(self._layout)<\/pre>\n<p>&nbsp;<\/p>\n<p>Here is my implementation of the MyGraphicsView class. \u00a0Derived from QGraphicsView<\/p>\n<pre>class MyGraphicsView(QGraphicsView):\r\n    def __init__(self):\r\n        QGraphicsView.__init__(self)\r\n        self.setRenderHints(QPainter.Antialiasing|QPainter.SmoothPixmapTransform)\r\n        self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)\r\n        self.setDragMode(QGraphicsView.ScrollHandDrag)\r\n    def wheelEvent(self,event):        \r\n        adj = (event.delta()\/120) * 0.1\r\n        self.scale(1+adj,1+adj)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This took me forever to figure out. \u00a0I noticed lots of people online had the same question but few got it working properly and some had to do a hack. \u00a0Here is the clean way to do this. \u00a0The trick is in setTransformationAnchor() and setDragMode() My setup code in my QDailogs __init__ function: imageFilename = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[60,59],"class_list":["post-554","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-python","tag-qt"],"_links":{"self":[{"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/posts\/554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/comments?post=554"}],"version-history":[{"count":5,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/posts\/554\/revisions"}],"predecessor-version":[{"id":558,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/posts\/554\/revisions\/558"}],"wp:attachment":[{"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/media?parent=554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/categories?post=554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/tags?post=554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}