Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to delete a widget at once in a QLayout?
Forum Update on Monday, May 27th 2025

How to delete a widget at once in a QLayout?

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 339 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • feiyuhuahuoF Offline
    feiyuhuahuoF Offline
    feiyuhuahuo
    wrote on last edited by
    #1

    I tried this way:

            print(self.main_ui.findChildren(QLabel))
            print(self.main_ui.verticalLayout_6.count())
            item = self.main_ui.verticalLayout_6.takeAt(0)
            item.widget().close()
            self.main_ui.verticalLayout_6.removeWidget(item.widget())
            print(self.main_ui.verticalLayout_6.count())
            print(self.main_ui.findChildren(QLabel))
    

    64b4451b-35a6-47cd-b733-3a0401597f5f-image.png
    Though it is deleted from the Layout, it still can be found in self.main_ui. I also tried widget.deleteLater() function, it can delete successfully in self.main_ui, but it can not delete at once.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What do you mean by "it cannot delete at once" ?

      deleteLater will trigger the deletion as soon as possible which is usually enough.

      What is your current issue ?
      What is your goal ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • feiyuhuahuoF Offline
        feiyuhuahuoF Offline
        feiyuhuahuo
        wrote on last edited by
        #3

        Hi, @SGaist
        e2028a90-d27f-45e4-b858-96d1090169d2-image.png
        My initial UI has a QGroupBox names group_box_ok, and you can see from line 739~740 that I want to delete it first. Then I want to add other QGroupBoxes according to class_names. For example, class_names is ('ok', 'dog', 'cat'), then I hope there's only group_box_ok, group_box_dog and group_box_cat in cls_aug_ui. But the result is:
        d5420eb8-4432-4825-bcb7-e43c10b0fea2-image.png
        So the old group_box_ok was not deleted at once. The following code raises bugs because there's a duplicated group_box_ok. And I must doing these things in the same function (slot). I want that cls_aug_ui.findChildren(QGroupBox) can not find the old group_box_ok .

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You can remove the parent of the widget you took out of the layout.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • feiyuhuahuoF Offline
            feiyuhuahuoF Offline
            feiyuhuahuo
            wrote on last edited by
            #5

            I found the parent of the widget need to be set as None before removing the widget.
            We can remove the widget by this way:

                    widget = self.main_ui.verticalLayout_6.takeAt(0).widget()
                    widget.setParent(None)
                    widget.close()
                    self.main_ui.verticalLayout_6.removeWidget(widget)
            
            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved