Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-38627

QLayout - Delete all items recursively

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 4.8.3, 5.3.1
    • Widgets: Layout
    • None

    Description

      Though QLayout, it is really handy if there is a function removing (& also deleting) all child items (which can be either layout itself or widget) managed by a QLayout, like this:

      void clearLayoutWidgets(QLayout* layout) // Not delete the layout itself!
      {
          if ((layout == nullptr) || (layout->isEmpty()))
              return;
      
          // !Note: QLayout inherits QLayoutItem
          QLayoutItem *item;
          while(item = layout->takeAt(0)) {
              // If item has sub-layouts:
              if (item->layout()) {
                  clearLayoutWidgets(item->layout()); // Not delete the layout itself!
                  delete item->layout(); // Delete outside if need!
              }
              // If item is a widget itself:
              else if (item->widget()) {
                  delete item->widget();
                  //OR layout->removeWidget(item->widget()) // item was already taken out of layout
              }
              // If item is just a pure empty layout:
              else {
                  // bool test = item->isEmpty();
              }
      
              // In any case: delete item itself!
              delete item;
          }
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            tadinu Tadinu
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes