I'm trying to overlay two QWidgets and have them both resize automatically when the window size is altered.
The background QWidget shows a grid with co-ordinates, the foreground QWidget contains other widgets (buttons etc). These are both set as children to the same QWidget. If I don't use a layout I can get them to overlap, but then I lose the useful auto resizing of the child widgets when the parent widget is resized.
My overall goal is creating an editor which will let the user design their own widgets for a dashboard. The grid background will be the gridlayout with their widgets in the foreground.
import sys
from PyQt5.QtWidgets import QApplication, QLabel, QPushButton, QWidget, \
QGridLayout
class Grid(QWidget):
def __init__(self, rows, columns, *args, **kwargs):
super().__init__(*args, **kwargs)
self.columns = columns
self.rows = rows
self.layout = QGridLayout()
self.layout.setContentsMargins(0,0,0,0)
self.layout.setSpacing(0)
for ii in range(self.rows):
for jj in range(self.columns):
label = QLabel('{}, {}'.format(ii,jj))
label.setStyleSheet("border: 1px solid grey; color: grey")
self.layout.addWidget(label, ii, jj, 1, 1)
self.setLayout(self.layout)
class Overlay(QWidget):
def __init__(self, rows, columns, *args, **kwargs):
super().__init__(*args, **kwargs)
self.rows = rows
self.columns = columns
self.layout = QGridLayout()
# self.layout.setContentsMargins(0,0,0,0)
# self.layout.setSpacing(0)
button_1 = QPushButton('yep')
button_2 = QPushButton('nope')
top_left_label = QLabel('tl')
bottom_right_label = QLabel('br')
self.layout.addWidget(button_1, 1, 1, 1, 4)
self.layout.addWidget(button_2, 4, 4, 2, 2)
self.layout.addWidget(top_left_label, 0, 0, 1, 1)
self.layout.addWidget(bottom_right_label, self.rows, self.columns, 1, 1)
self.setLayout(self.layout)
class Parent_Dashboard_Template(QWidget):
def __init__(self, rows, columns, *args, **kwargs):
super().__init__(*args, **kwargs)
self.rows = rows
self.columns = columns
self.setMinimumSize(400, 200)
self.layout = QGridLayout()
self.layout_2 = QGridLayout()
self.grid = Grid(self.rows, self.columns, parent=self)
# self.layout.addWidget(self.grid)
# self.setLayout(self.layout)
# self.grid.showMaximized()
self.overlay = Overlay(self.rows, self.columns, parent=self)
# self.layout_2.addWidget(self.overlay)
# self.setLayout(self.layout_2)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Parent_Dashboard_Template(10, 10)
window.show()
app.exec_()