python - Odd anchoring behavior when zooming in PyQt/Pyside -
consider following code:
# pyside import qtcore, qtgui, qtnetwork, qtwebkit pyqt4 import qtcore, qtgui, qtnetwork, qtwebkit import os import sys class imageview(qtgui.qgraphicsview): def __init__(self, file): super().__init__() self.setscene(qtgui.qgraphicsscene()) self.settransformationanchor(self.anchorundermouse) self.sethorizontalscrollbarpolicy(qtcore.qt.scrollbaralwaysoff) self.setverticalscrollbarpolicy(qtcore.qt.scrollbaralwaysoff) item = qtgui.qgraphicspixmapitem(qtgui.qpixmap(file)) self.scene().additem(item) self.scene().setscenerect(self.scene().itemsboundingrect()) self.fitinview(self.scene().scenerect(), qtcore.qt.keepaspectratio) def wheelevent(self, event): print(self.transformationanchor()) self.scale(1.1, 1.1) class host(qtgui.qtabwidget): def removetab(self, args): self.widget(args).deletelater() super().removetab(args) def new_image(self, image): view = imageview(image) view.file = image tab = self.addtab(view, image) self.setcurrentindex(tab) def keypressevent(self, event): self.removetab(self.currentindex()) def mousepressevent(self, event): self.removetab(self.currentindex()) image = 'selenium1.png' app = qtgui.qapplication([]) host = host() host.new_image(image) host.new_image(image) host.show() app.exec_()
i create qtabwidget 2 qgraphicsview's in it, assign both image , set anchoring anchorundermouse. tab can closed click inside or keypress.
when try zooming on tab selected when app starts, correctly zooms cursor. if switch other tab, zooms cursor well. if close tab keypress , switch tab, still zooms cursor. if close tab mouse click instead, zoom switches center-anchoring, though console still logs transformationanchor being set anchorundermouse. however, if move mouse anywhere outside view , in or alt-tab out , in, anchoring again becomes correct. happens both on pyqt , pyside.
i narrowed down line self.widget(args).deletelater()
, removing helps, without images don't garbage-collected after container tab closed, , don't understand how relevant. why work this?
wiki
Comments
Post a Comment