![]() |
Delete Qt Layout manager - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Delete Qt Layout manager (/thread-31937.html) |
Delete Qt Layout manager - deanhystad - Jan-10-2021 I cannot find a way to change the layout manager for a Qt widget. In the code below I first set the layout manager to a horizontal box layout and then try to set it to a grid layout. From the output you can see that the layout remains a horizontal box layou import sys import PySide6.QtWidgets as QtWidgets class Test(QtWidgets.QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setLayout(QtWidgets.QHBoxLayout()) self.setLayout(QtWidgets.QGridLayout()) print(self.layout()) QtWidgets.QApplication(sys.argv) Test() Qt documentation says you have to delete the old layout before setting a new layout. So I tried the code below which gives me a syntax error on line 9 saying "Cannot delete function call".class Test(QtWidgets.QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setLayout(QtWidgets.QHBoxLayout()) del self.layout() print(self.layout())So I use a local variable to reference the layout widget and tried again. class Test(QtWidgets.QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setLayout(QtWidgets.QHBoxLayout()) temp = self.layout() delete temp print(self.layout())This code runs, but it does not remove the layout widget. I still get the QHBoxLayout when printing self.layout() Does anyone have an idea how I can replace the existing layout? RE: Delete Qt Layout manager - Axel_Erfurt - Jan-10-2021 You should define different Layouts and use QMainWindow for example import sys import PyQt5.QtWidgets as QtWidgets class Test(QtWidgets.QMainWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.layout = QtWidgets.QVBoxLayout() self.btn1 = QtWidgets.QPushButton("HLayout") self.btn1.clicked.connect(self.setLayoutH) self.btn2 = QtWidgets.QPushButton("GLayout") self.btn2.clicked.connect(self.setLayoutG) self.layout.addWidget(self.btn1) self.layout.addWidget(self.btn2) w = QtWidgets.QWidget() w.setLayout(self.layout) self.setCentralWidget(w) def setLayoutG(self): # gridlayout gridlayout = QtWidgets.QGridLayout() btn1 = QtWidgets.QPushButton("HLayout") gridlayout.addWidget(btn1) btn1.clicked.connect(self.setLayoutH) self.cwg = QtWidgets.QWidget() self.cwg.setLayout(gridlayout) self.setCentralWidget(self.cwg) def setLayoutH(self): # hlayout hlayout = QtWidgets.QHBoxLayout() btn2 = QtWidgets.QPushButton("GLayout") btn2.clicked.connect(self.setLayoutG) hlayout.addWidget(btn2) self.editor = QtWidgets.QTextEdit() hlayout.addWidget(self.editor) self.cwh = QtWidgets.QWidget() self.cwh.setLayout(hlayout) self.setCentralWidget(self.cwh) if __name__ == '__main__': import sys app = QtWidgets.QApplication(sys.argv) win = Test() win.setGeometry(0, 0, 400, 200) win.setWindowTitle("MyApp" + "[*]") win.show() sys.exit(app.exec_()) RE: Delete Qt Layout manager - deanhystad - Jan-10-2021 I'm not doing this for a main window, otherwise that would be a great idea. |