Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

python - replacing layout on a QWidget with another layout

I have a widget which changes when an option is toggled. This invalidates all layouts and widgets. I keep list of all layouts, so I can delete them using something similar to this answer:

class MyWidget(QFrame):
   # ...
   def reLayout(self):
      def deleteLayoutChilds(l):
         while l.count():
            item=l.takeAt(0)
            widget=item.widget()
            if widget: widget.deleteLater()
            else: deleteLayoutChilds(item.layout())
      for l in self.allLayouts: deleteLayoutChilds(l)

      # now install the new layout
      ##
      ## how to delete the old layout first?
      l=self.layout(); del l # no effect
      #
      layout=QGridLayout(self)
      ## warning: QLayout: Attempting to add QLayout "" to MyWidget "", which already has a layout.

How can I get rid of the old layout and set the new one?


The documentation is quite terse and apparently not directly applicable to python:

QWidget.setLayout (self, QLayout)

The QLayout argument has it's ownership transferred to Qt.

Sets the layout manager for this widget to layout.

If there already is a layout manager installed on this widget, QWidget won't let you install another. You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout.

If layout is the layout manger on a different widget, setLayout() will reparent the layout and make it the layout manager for this widget.

Example:

 QVBoxLayout *layout = new QVBoxLayout;
 layout->addWidget(formWidget);
 setLayout(layout);

An alternative to calling this function is to pass this widget to the layout's constructor.

The QWidget will take ownership of layout.

See also layout() and Layout Management.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can simply reparent the layout to a temporary widget:

def reLayout(self):
    QWidget().setLayout(self.layout())
    layout = QGridLayout(self)
    ...

That will reparent all the child widgets to that temporary object, and that object is deleted immediately along with its new children because we don't keep a reference to it.

But the typical way to have multiple layouts for a single widget and to be able to switch between them is to use a QStackedWidget or QStackedLayout.


And if you still need the answer to that secondary question:

How to delete the old layout first?

It seems you can't delete directly a QObject which has a parent, because the parent is keeping a reference to that object. But you can add the object to a temporary QObjectCleanupHandler which, like the above solution, will be deleted with the object(s) it contains immediately:

QObjectCleanupHandler().add(self.layout())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...