There is a widget (Forma ()), in it a couple of buttons in layout`e. I add:

sub = QMdiSubWindow() sub.setAttribute(Qt.WA_DeleteOnClose) sub.setWidget(Forma()) self.mdi.addSubWindow(sub) sub.show() 

enter image description here It works, but when I stretch the window, my widget remains in place. But, in the next. example:

  sub = QMdiSubWindow() sub.setAttribute(Qt.WA_DeleteOnClose) sub.setWidget(QPushButton('Show')) self.mdi.addSubWindow(sub) sub.show() 

enter image description here I get the desired result (when stretching the window, the button expands). How to do this with my widget?

    1 answer 1

    It works exactly the same. Here is an example:

     import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class Forma(QWidget): # Есть свой виджет(Forma()) def __init__(self): super().__init__() self.button1 = QPushButton('Forma: Show button1...', self) # в ней пару кнопочек в layout`e self.button2 = QPushButton('Forma: Show button2...', self) # в ней пару кнопочек в layout`e self.grid = QGridLayout(self) self.grid.addWidget(self.button1, 0, 0) self.grid.addWidget(self.button2, 1, 0) class MainWindow(QMainWindow): count = 0 def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.mdi = QMdiArea() self.setCentralWidget(self.mdi) bar = self.menuBar() file = bar.addMenu("File") file.addAction("New") file.addAction("cascade") file.addAction("Tiled") file.triggered[QAction].connect(self.windowaction) self.setWindowTitle("MDI demo") sub = QMdiSubWindow() sub.setAttribute(Qt.WA_DeleteOnClose) sub.setWidget(QPushButton('Show')) sub.setWindowTitle("PushButton "+str(MainWindow.count)) self.mdi.addSubWindow(sub) sub.show() def windowaction(self, q): print("triggered-> q ->", q.text()) if q.text() == "New": MainWindow.count = MainWindow.count+1 sub = QMdiSubWindow() sub.setAttribute(Qt.WA_DeleteOnClose) sub.setWidget(Forma()) # <------- sub.setWindowTitle("subwindow"+str(MainWindow.count)) self.mdi.addSubWindow(sub) sub.show() if q.text() == "cascade": self.mdi.cascadeSubWindows() if q.text() == "Tiled": self.mdi.tileSubWindows() if __name__ == '__main__': app = QApplication(sys.argv) ex = MainWindow() ex.show() sys.exit(app.exec_()) 

    enter image description here