PyQt5 QGraphicsView 对象没有属性'resetMatrix'?
PyQt5 QGraphicsView object has no attribute 'resetMatrix'?
考虑这个 PyQT5 示例,我们称它为 test.py
(对我来说,在 Ubuntu 18.04 上的 python2
和 python3
下表现相同):
#!/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_())
如果我运行按原样,运行没问题。
如果我取消注释 self.resetMatrix()
行,程序将失败并显示:
$ 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'
但我觉得这很奇怪,因为 PhotoViewer
继承自 QGraphicsView
,调用 PhotoViewer.scale()
这是一个 QGraphicsView
方法显然不是问题 - 而且 文档表明调用 QGraphicsView()->resetMatrix()
应该是可能的,并且它还记录了两者:
- 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
我犯了什么错误 - 为什么在这种情况下我不能调用 resetMatrix
;我应该怎么做才能调用这个函数?
看来是PyQt5的bug,我用PySide2测试过,可以正常使用。但是有一个解决方法,如果您检查 source code you see that the the resetMatrix() method calls only resetTransform(),那么它会使用该方法。
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)
考虑这个 PyQT5 示例,我们称它为 test.py
(对我来说,在 Ubuntu 18.04 上的 python2
和 python3
下表现相同):
#!/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_())
如果我运行按原样,运行没问题。
如果我取消注释 self.resetMatrix()
行,程序将失败并显示:
$ 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'
但我觉得这很奇怪,因为 PhotoViewer
继承自 QGraphicsView
,调用 PhotoViewer.scale()
这是一个 QGraphicsView
方法显然不是问题 - 而且 QGraphicsView()->resetMatrix()
应该是可能的,并且它还记录了两者:
- 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
我犯了什么错误 - 为什么在这种情况下我不能调用 resetMatrix
;我应该怎么做才能调用这个函数?
看来是PyQt5的bug,我用PySide2测试过,可以正常使用。但是有一个解决方法,如果您检查 source code you see that the the resetMatrix() method calls only resetTransform(),那么它会使用该方法。
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)