考虑这个Pyqt5例子,我们称之为
test.py
(对我来说,在两种情况下都是一样的
python2
和
python3
在Ubuntu 18.04上):
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.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()
AttributeError: 'PhotoViewer' object has no attribute 'resetMatrix'
但我觉得这很奇怪,因为
PhotoViewer
继承自
QGraphicsView
呼唤
PhotoViewer.scale()
哪个是
QLogigsVIEW
方法显然不是问题-而且
How to reset the scale in QGraphicsView?
调用的文档
QGraphicsView()->resetMatrix()
应该是可能的,而且它也记录了以下两个方面:
我犯了什么错误-为什么我不能打电话
resetMatrix
在这种情况下,我应该怎么做才能调用这个函数呢?