代码之家  ›  专栏  ›  技术社区  ›  sdaau

pyqt5 qgraphicsView对象没有属性“resetmatrix”?

  •  1
  • sdaau  · 技术社区  · 6 年前

    考虑这个Pyqt5例子,我们称之为 test.py (对我来说,在两种情况下都是一样的 python2 python3 在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_())
    

    如果我按原样运行,它运行良好,没有问题。

    如果我取消注释 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() 哪个是 QLogigsVIEW 方法显然不是问题-而且 How to reset the scale in QGraphicsView? 调用的文档 QGraphicsView()->resetMatrix() 应该是可能的,而且它也记录了以下两个方面:

    我犯了什么错误-为什么我不能打电话 resetMatrix 在这种情况下,我应该怎么做才能调用这个函数呢?

    1 回复  |  直到 6 年前
        1
  •  1
  •   eyllanesc Yonghwan Shin    6 年前

    好像是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)