Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Re-populating a layout's widgets
Forum Updated to NodeBB v4.3 + New Features

Re-populating a layout's widgets

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 184 Views 3 Watching
  • 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.
  • C Offline
    C Offline
    CJF the Elder
    wrote last edited by
    #1

    I want to say to all of you many thanks for helping me get up to speed on Qt. I am about 7 years late it seems.

    All of the docs and the many topics I've read say to do this if I want to repop a layout. I must first remove the ones currently in place:

        while ( m_pane->count() != 0 ) {
            item = m_pane->itemAt(0);
            if ( item->widget() ) {
                delete item->widget();
                //delete item;
            }
        }
    

    But this code only runs if the "delete item" call is remarked out. If an attempt is made to delete the item, the application crashes. Strangely, it works fine without deleting the item, but I suspect that tiny amounts of memory are being leaked, not sure. Has the behavior been changed? Do I need to disconnect the slot and context menu attached to them?

    Pl45m4P 1 Reply Last reply
    0
    • C CJF the Elder

      I want to say to all of you many thanks for helping me get up to speed on Qt. I am about 7 years late it seems.

      All of the docs and the many topics I've read say to do this if I want to repop a layout. I must first remove the ones currently in place:

          while ( m_pane->count() != 0 ) {
              item = m_pane->itemAt(0);
              if ( item->widget() ) {
                  delete item->widget();
                  //delete item;
              }
          }
      

      But this code only runs if the "delete item" call is remarked out. If an attempt is made to delete the item, the application crashes. Strangely, it works fine without deleting the item, but I suspect that tiny amounts of memory are being leaked, not sure. Has the behavior been changed? Do I need to disconnect the slot and context menu attached to them?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote last edited by Pl45m4
      #2

      @CJF-the-Elder said in Re-populating a layout's widgets:

      I suspect that tiny amounts of memory are being leaked, not sure.

      Not if your item has a QObject parent.

      Has the behavior been changed?

      No.

      Do I need to disconnect the slot and context menu attached to them?

      Also no.

      A cleaner and working solution is even listed in the Qt Documentation:
      QLayout::takeAt(int index) is the key.

      QLayoutItem *child;
      while ((child = layout->takeAt(0)) != nullptr) {
          ...
          delete child->widget(); // delete the widget
          delete child;   // delete the layout item
      }
      

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      1
      • C Offline
        C Offline
        CJF the Elder
        wrote last edited by CJF the Elder
        #3

        Thanks, that fixed it. And thanks for insisting I try something that didn't work for me prior. When I first tried the code from the doc as you show, I must have been doing something else wrong.

        1 Reply Last reply
        0
        • C CJF the Elder has marked this topic as solved
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote last edited by
          #4

          Hi,

          Depending on the complexity of your layout it might also be worth considering putting "grouped widgets" in their own QWidget subclass to simplify these kind of loops.

          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
          • C Offline
            C Offline
            CJF the Elder
            wrote last edited by
            #5

            Suggestion duly noted. Since the widget(s) the code was adding and removing was the first QWidget I ever wrote, it definitely needs revisiting.

            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