Self Define QGraphicsView not constrained by QGridLayout
-
I want four of my QGraphicsView to be same size, but when I change one to self define QGraphicsView instance, the layout would be broken.
from PhotoViewer import PhotoViewer from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.gridLayout = QtWidgets.QGridLayout(self.centralwidget) self.gridLayout.setObjectName("gridLayout") self.frame = QtWidgets.QFrame(self.centralwidget) self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel) self.frame.setFrameShadow(QtWidgets.QFrame.Raised) self.frame.setObjectName("frame") self.gridLayout_2 = QtWidgets.QGridLayout(self.frame) self.gridLayout_2.setObjectName("gridLayout_2") self.graphicsView_2 = QtWidgets.QGraphicsView(self.frame) self.graphicsView_2.setObjectName("graphicsView_2") self.gridLayout_2.addWidget(self.graphicsView_2, 0, 1, 1, 1) self.graphicsView = QtWidgets.QGraphicsView(self.frame) self.graphicsView.setObjectName("graphicsView") self.gridLayout_2.addWidget(self.graphicsView, 0, 0, 1, 1) self.graphicsView_3 = PhotoViewer(self.frame) self.graphicsView_3.setObjectName("graphicsView_3") # self.graphicsView_3.adjustSize() print(self.graphicsView_3.size()) self.gridLayout_2.addWidget(self.graphicsView_3, 1, 0, 1, 1) self.graphicsView_4 = QtWidgets.QGraphicsView(self.frame) self.graphicsView_4.setObjectName("graphicsView_4") self.gridLayout_2.addWidget(self.graphicsView_4, 1, 1, 1, 1) self.gridLayout.addWidget(self.frame, 0, 0, 1, 1) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
and the self define is a class inherit QGraphicsView and just override mousePressedEvent, mouseReleasedEvent and mouseMoveEvent.
class PhotoViewer(QtWidgets.QGraphicsView): pass
this is my outcome, black one is which I used PhotoViewer.
and if I don't use QGraphicsView instead of PhotoViewer would be like
I am wondering If I override sizeHint(), or adjustSize(), to let my object to fit the layout. -
Layout is not broken, if you mean that size of the is different when you use different widgets in the layouts, it is probably because of different size policies or for example minimumSize set to them. Check for example if minimum size is specified for your PhotoViewer
-
I print them out, and minimumSize, and sizePolicy are both same. -
Hi,
Why do you use two layouts ?
-
But why not set your frame as central widget ?
You are using one widget and one layout for nothing.
-
@SGaist ok I see, but I still can't get the size after layout(this return QtCore.QSize())
self.graphicsView_3.adjustSize() print(self.graphicsView_3.size())
full code
from PyQt5 import QtCore, QtGui, QtWidgets from PhotoViewer import PhotoViewer class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.gridLayout = QtWidgets.QGridLayout(self.centralwidget) self.gridLayout.setObjectName("gridLayout") self.graphicsView_2 = QtWidgets.QGraphicsView(self.centralwidget) self.graphicsView_2.setObjectName("graphicsView_2") self.gridLayout.addWidget(self.graphicsView_2, 2, 0, 1, 1) self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget) self.graphicsView.setObjectName("graphicsView") self.gridLayout.addWidget(self.graphicsView, 1, 0, 1, 1) self.graphicsView_3 = PhotoViewer(self.centralwidget) self.graphicsView_3.adjustSize() print(self.graphicsView_3.size()) self.graphicsView_3.setObjectName("graphicsView_3") self.gridLayout.addWidget(self.graphicsView_3, 1, 1, 1, 1) self.graphicsView_4 = QtWidgets.QGraphicsView(self.centralwidget) self.graphicsView_4.setObjectName("graphicsView_4") self.gridLayout.addWidget(self.graphicsView_4, 2, 1, 1, 1) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())