How to prevent Qt buttons from appearing in a separate frame?

I am working on a PyQt application. There is currently a status bar (defined as QWidget) that contains QHBoxLayout. This layout is often updated with help QPushButtoncreated by another part of the application.

Whenever the buttons that appear should change (which is pretty common), the update effect is called. Existing buttons are removed from the layout (by calling layout.removeWidget(button), then button.setParent(None)), and new buttons are added to the layout.

This usually works. But sometimes, when I call button.setParent(None)the button for deletion, it makes it jump out of the application and start swimming in its own frame.

How to remove a button from the layout and make sure that it does not start?

+3
source share
2 answers

You must call the method close(). If you want to be deleted when it is closed, you can set the attribute Qt.WA_DeleteOnClose:

button.setAttribute(Qt.WA_DeleteOnClose)
+2
source

Try calling QWidget::hide()the button before removing it from the layout if you do not want to delete your button.

+2
source

Source: https://habr.com/ru/post/1732810/


All Articles