Why the sizePolicy doesn’t affect on widgets that aren’t in layout?
here is an example:
from PyQt5 import QtWidgets app = QtWidgets.QApplication([]) window = QtWidgets.QWidget() window.setGeometry(50, 50, 500, 300) test_widget = QtWidgets.QWidget(window) test_widget.setMinimumSize(100, 100) test_widget.setStyleSheet("background-color:red") size_policy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum) test_widget.setSizePolicy(size_policy) window.show() app.exec()
But that doesn’t work, if you changed the main window size the red box still has the same size.
So how can I make that red box resizeable when the parent (main window) is changing.
NOTE: I don’t want to use Layouts for some reason.
Advertisement
Answer
I’m not sure I completely understand your GUI design but you should note that a single cell (as defined by a row/column pair) in a QGridLayout
can be occupied by more than a single widget. A (very) simple example demonstrates this…
#!/usr/local/bin/python3 import os import sys from PySide2.QtWidgets import QApplication, QGridLayout, QLabel, QPushButton, QWidget from PySide2.QtCore import Qt class widget(QWidget): def __init__ (self, parent = None): super(widget, self).__init__(parent) gl = QGridLayout(self) pb = QPushButton("Show/Hide Menu") self.menu = QLabel("Menu goes here...") self.menu.setAlignment(Qt.AlignCenter) self.menu.setStyleSheet("background-color: #40800000;") canvas = QLabel("Canvas") canvas.setAlignment(Qt.AlignCenter) canvas.setStyleSheet("background-color: #40000080;") gl.addWidget(pb, 0, 0) gl.addWidget(canvas, 0, 0, 2, 2) pb.raise_() pb.clicked.connect(self.toggle_menu) gl.addWidget(self.menu, 1, 0) self.menu.hide() def toggle_menu (self, checked): self.menu.setVisible(not self.menu.isVisible()) if __name__ == '__main__': app = QApplication([]) w = widget() w.show() app.exec_()
[I’ve used PySide2
as I don’t have PyQt5
installed.]
So if I have understood correctly then I don’t see why you can’t make use of a QGridLayout
. Could save you a lot of work.