QGraphicsView 开始时大小错误

QGraphicsView wrong size at start

我有一个问题,我制作了一个内部有 2 个矩形的 QGraphicsView。我希望它们在我调整对话框大小时也能调整大小,所以我重写了一个对话框 class。它有效,但在开始时我有错误大小的矩形

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsEllipseItem
import sys

# Redéfinition de dialog pour utiliser resize event

class MyDialog (QtWidgets.QDialog) :
    def resizeEvent(self, event):
        print("resize")
        #print(view.sceneRect())

        view.fitInView(scene.sceneRect(), QtCore.Qt.KeepAspectRatio) ######## I DONT KNOW WHY THIS LINE MAKE THE VIEW TOO SMALL AT START
        QtWidgets.QDialog.resizeEvent(self, event)
        #view.fitInView(rectangle)
        
class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(556, 580)
        self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName("verticalLayout")
        self.graphicsView = QtWidgets.QGraphicsView(Dialog)
        self.graphicsView.setObjectName("graphicsView")
        self.verticalLayout.addWidget(self.graphicsView)

        global view
        view = self.graphicsView
        triangle = QtWidgets.QGraphicsRectItem(10,10,200,200)
        rectangle  = QtWidgets.QGraphicsRectItem(100,100,200,200)
        global scene
        scene = QtWidgets.QGraphicsScene()

        scene.addItem(rectangle)
        scene.addItem(triangle)
        self.graphicsView.setScene(scene)


        #self.graphicsView.fitInView(scene.sceneRect(), QtCore.Qt.KeepAspectRatio)
        #view.fitInView(scene.sceneRect(), QtCore.Qt.KeepAspectRatio)

        
        self.graphicsView_2 = QtWidgets.QGraphicsView(Dialog)
        self.graphicsView_2.setObjectName("graphicsView_2")
        self.verticalLayout.addWidget(self.graphicsView_2)
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.verticalLayout.addWidget(self.buttonBox)

        self.retranslateUi(Dialog)
        self.buttonBox.accepted.connect(Dialog.accept)
        self.buttonBox.rejected.connect(Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = MyDialog()
    ui = Ui_Dialog()
    
    ui.setupUi(Dialog)
    
    Dialog.show()
    sys.exit(app.exec_())

当我启动 QDialog 时它看起来像这样

当我调整 window 的大小时,我希望它看起来像

注意:建议不要修改 Qt Designer 生成的代码,因此要使我的答案生效,您必须使用 pyuic5 重新生成文件:pyuic5 your_file.ui -o gui.py -x.

所以fitInView方法使用了一些属性直到它使QGraphicsView可见才更新,所以解决方法是验证它是否可见:

main.py

from PyQt5 import QtCore, QtGui, QtWidgets

from gui import Ui_Dialog


class MyDialog(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)

        triangle = QtWidgets.QGraphicsRectItem(10, 10, 200, 200)
        rectangle = QtWidgets.QGraphicsRectItem(100, 100, 200, 200)
        scene = QtWidgets.QGraphicsScene()

        scene.addItem(rectangle)
        scene.addItem(triangle)
        self.graphicsView.setScene(scene)

    def resizeEvent(self, event):
        if self.isVisible():
            self.graphicsView.fitInView(
                self.graphicsView.scene().sceneRect(), QtCore.Qt.KeepAspectRatio
            )
        super().resizeEvent(event)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MyDialog()
    w.show()
    sys.exit(app.exec_())