【发布时间】:2019-06-16 03:12:31
【问题描述】:
考虑这个 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 方法显然不是问题 - 并且How to reset the scale in QGraphicsView? 文件调用QGraphicsView()->resetMatrix() 应该是可能的,并且两者都记录在案:
- http://pyqt.sourceforge.net/Docs/PyQt4/qgraphicsview.html#resetMatrix
- http://pyqt.sourceforge.net/Docs/PyQt5/api/QtWidgets/qgraphicsview.html -> “可以在 here 找到 C++ 文档。” -> https://doc.qt.io/qt-5/qgraphicsview.html#resetMatrix
我犯了什么错误——在这种情况下为什么我不能打电话给resetMatrix;我应该怎么做才能调用这个函数?
【问题讨论】:
标签: python pyqt pyqt5 qgraphicsview