QGraphicsView wrong size at start

Viewed 91

I have a problem, i have made a QGraphicsView with 2 rectangles inside. I wanted them to resize when i resize the Dialog so i rewrite a dialog class. It works but at start i have the wrong sized rectangle

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_())

When i start the QDialog it looks like this

enter image description here

And when i resize the window it looks like i wanted it to look like enter image description here

1 Answers

Note: It is recommended not to modify the code generated by Qt Designer, so for my answer to work you must regenerate the file using pyuic5: pyuic5 your_file.ui -o gui.py -x.

So that the fitInView method uses some property that is not updated until it makes the QGraphicsView visible, so the solution is to verify that it is visible:

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_())
Related