Python Forum
Delete Qt Layout manager
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete Qt Layout manager
#1
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()
Output:
<PySide6.QtWidgets.QHBoxLayout(0x1731138e8a0) at 0x000001731242AA00>
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?
Reply
#2
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_())
Reply
#3
I'm not doing this for a main window, otherwise that would be a great idea.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How delete Qt Layout manager from another method? hobbyist 4 2,795 Sep-19-2021, 07:57 PM
Last Post: Axel_Erfurt
  pyqt5 layout Nickd12 8 3,501 Jan-18-2021, 09:09 AM
Last Post: Axel_Erfurt
  Python GUI layout off between different OS shift838 5 3,719 Jan-02-2019, 02:53 AM
Last Post: shift838
  [Tkinter] grid layout neech 8 17,660 Oct-14-2016, 07:06 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020