PyQt5 QGraphicsView对象没有属性“resetMatrix”?

2024-09-27 07:20:43 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑一下这个PyQT5示例,我们将其称为test.py(对我来说,在ubuntu18.04上,python2和{}下的行为相同):

#!/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()行,则程序将失败,并显示:

^{pr2}$

但我觉得这很奇怪,因为PhotoViewer继承了QGraphicsView,调用PhotoViewer.scale()这是一个QGraphicsView方法显然不是问题,而且调用QGraphicsView()->resetMatrix()的{a1}文档应该是可能的,而且这两个文档都有文档记录:

我犯的错误是什么-为什么在这种情况下我不能调用resetMatrix;我应该怎么做才能调用这个函数?在


Tags: from文档pytestimportselfinitmain
1条回答
网友
1楼 · 发布于 2024-09-27 07:20:43

这似乎是PyQt5的一个bug,我用PySide2测试过它,它工作正常。但是有一个解决方法,如果您检查source code,您会发现resetMatrix()方法只调用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)

相关问题 更多 >

    热门问题