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


Messages In This Thread
Delete Qt Layout manager - by deanhystad - Jan-10-2021, 07:16 PM
RE: Delete Qt Layout manager - by Axel_Erfurt - Jan-10-2021, 08:20 PM
RE: Delete Qt Layout manager - by deanhystad - Jan-10-2021, 08:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How delete Qt Layout manager from another method? hobbyist 4 2,979 Sep-19-2021, 07:57 PM
Last Post: Axel_Erfurt
  pyqt5 layout Nickd12 8 3,855 Jan-18-2021, 09:09 AM
Last Post: Axel_Erfurt
  Python GUI layout off between different OS shift838 5 3,906 Jan-02-2019, 02:53 AM
Last Post: shift838
  [Tkinter] grid layout neech 8 18,197 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