Jan-10-2021, 07:16 PM
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
Does anyone have an idea how I can replace the existing layout?
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?