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. How to remove children from a widget?
Forum Updated to NodeBB v4.3 + New Features

How to remove children from a widget?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 8.5k 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.
  • K Offline
    K Offline
    kitfox
    wrote on last edited by
    #1

    I'm having some trouble with crashes when removing child widgets from a parent widget.

    I'm creating a control that is used to display the content of a list of data objects in a way that will allow the user to select one of the objects and edit the data in it.

    I'm doing this by creating a scrollArea that contains a holder QWidget with a horizontal layout. I then create one of my MixerTrack UI widgets to wrap one mixer track for each track I want the user to edit and add it to my holder. Later, if the track list changes, I remove and delete the holder widget, create a new one, add it to my scrollarea and then create new MixerTrack widgets to wrap my new list data.

    This was working well for a while, but recently I've been getting crashes when I perform a delete on my holder widget. I'm not sure why and the stack trace provides no clue. There is nothing happening in the destructor of my widget that should be causing a problem. Commenting out the delete operation prevents the crash, but messes up the display because the old widgets aren't being removed anymore.

    Am I going about this right?

    void Mixer::updateTracks()
          {
          if (trackHolder) {
                trackAreaLayout->removeWidget(trackHolder);
                delete trackHolder;
                trackHolder = 0;
                }
    
          if (!_score)
                return;
    
          trackHolder = new QWidget();
          QHBoxLayout* holderLayout = new QHBoxLayout();
          trackHolder->setLayout(holderLayout);
    
          trackAreaLayout->addWidget(trackHolder);
    
          for (Part* part : _score->parts()) {
    ...
                MixerTrackPart* track = new MixerTrackPart(this, mti, expanded);
                holderLayout->addWidget(track);
    ...
          }
          update()
    }
    
    J.HilkJ 1 Reply Last reply
    0
    • K kitfox

      I'm having some trouble with crashes when removing child widgets from a parent widget.

      I'm creating a control that is used to display the content of a list of data objects in a way that will allow the user to select one of the objects and edit the data in it.

      I'm doing this by creating a scrollArea that contains a holder QWidget with a horizontal layout. I then create one of my MixerTrack UI widgets to wrap one mixer track for each track I want the user to edit and add it to my holder. Later, if the track list changes, I remove and delete the holder widget, create a new one, add it to my scrollarea and then create new MixerTrack widgets to wrap my new list data.

      This was working well for a while, but recently I've been getting crashes when I perform a delete on my holder widget. I'm not sure why and the stack trace provides no clue. There is nothing happening in the destructor of my widget that should be causing a problem. Commenting out the delete operation prevents the crash, but messes up the display because the old widgets aren't being removed anymore.

      Am I going about this right?

      void Mixer::updateTracks()
            {
            if (trackHolder) {
                  trackAreaLayout->removeWidget(trackHolder);
                  delete trackHolder;
                  trackHolder = 0;
                  }
      
            if (!_score)
                  return;
      
            trackHolder = new QWidget();
            QHBoxLayout* holderLayout = new QHBoxLayout();
            trackHolder->setLayout(holderLayout);
      
            trackAreaLayout->addWidget(trackHolder);
      
            for (Part* part : _score->parts()) {
      ...
                  MixerTrackPart* track = new MixerTrackPart(this, mti, expanded);
                  holderLayout->addWidget(track);
      ...
            }
            update()
      }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      hi @kitfox
      have tried it with deleteLater() istead of delete? It‘s the recommanded way for QWidget/QObject based classes, may already solve the issue.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      K 1 Reply Last reply
      1
      • J.HilkJ J.Hilk

        hi @kitfox
        have tried it with deleteLater() istead of delete? It‘s the recommanded way for QWidget/QObject based classes, may already solve the issue.

        K Offline
        K Offline
        kitfox
        wrote on last edited by
        #3

        @J.Hilk deleteLater() worked! Thanks so much.

        1 Reply Last reply
        1

        • Login

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