Issue
Consider this PyQT5 example, let's call it test.py
(for me, behaves the same under both python2
and python3
on Ubuntu 18.04):
#!/usr/bin/env python
from __future__ import print_function
import sys, os
from PyQt5 import QtCore, QtWidgets, QtGui
class PhotoViewer(QtWidgets.QGraphicsView):
def __init__(self, parent):
super(PhotoViewer, self).__init__(parent)
self.parent = parent
#self.resetMatrix() # SO: 39101834, but "AttributeError: 'PhotoViewer' object has no attribute 'resetMatrix'"
self.scale(1.0, 1.0)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setWindowTitle("test.py")
self.setMinimumWidth(1000)
self.setMinimumHeight(600)
self.viewer = PhotoViewer(self)
wid = QtWidgets.QWidget(self)
self.setCentralWidget(wid)
VBlayout = QtWidgets.QVBoxLayout()
VBlayout.addWidget(self.viewer)
wid.setLayout(VBlayout)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
main = MainWindow()
main.show()
sys.exit(app.exec_())
If I run it as is, it runs fine, without a problem.
If I uncomment the commented self.resetMatrix()
line, then the program fails with:
$ python test.py
Traceback (most recent call last):
File "test.py", line 29, in <module>
main = MainWindow()
File "test.py", line 20, in __init__
self.viewer = PhotoViewer(self)
File "test.py", line 11, in __init__
self.resetMatrix() # SO: 39101834, but "AttributeError: 'PhotoViewer' object has no attribute 'resetMatrix'"
AttributeError: 'PhotoViewer' object has no attribute 'resetMatrix'
But this I find rather bizarre, because PhotoViewer
inherits from QGraphicsView
, calling PhotoViewer.scale()
which is a QGraphicsView
method is clearly not a problem - and How to reset the scale in QGraphicsView? documents that calling QGraphicsView()->resetMatrix()
should be possible, and also it is documented for both:
- http://pyqt.sourceforge.net/Docs/PyQt4/qgraphicsview.html#resetMatrix
- http://pyqt.sourceforge.net/Docs/PyQt5/api/QtWidgets/qgraphicsview.html -> "The C++ documentation can be found here." -> https://doc.qt.io/qt-5/qgraphicsview.html#resetMatrix
What is the mistake I'm making - why cannot I call resetMatrix
in this case; and what should I do to be able to call this function?
Solution
It seems that it is a bug of PyQt5, I have tested it with PySide2 and it works correctly. But there is a workaround, if you check the source code you see that the the resetMatrix() method calls only resetTransform() so it uses that method.
class PhotoViewer(QtWidgets.QGraphicsView):
def __init__(self, parent):
super(PhotoViewer, self).__init__(parent)
self.parent = parent
self.resetTransform() # self.resetMatrix()
self.scale(1.0, 1.0)
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.