Results 1 to 5 of 5

Thread: Remove widget from a QList

  1. #1
    Join Date
    Aug 2009
    Posts
    50
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Remove widget from a QList

    Hello; I wonder if anyone can help with this...

    In my GUI I have a fairly complicated matrix of widgets in a QGridLayout. From time to time, I need to add or remove rows of widgets from the GUI.

    I store pointers to each widget in a set of QLists, one for each type of widget. Adding widgets is no problem. I simply use:

    Qt Code:
    1. // First, remove from the layout all the widgets in rows below the one to be inserted
    2. ...
    3. // Then, add a new widget in row 'i'
    4. MyWidgetList.insert(i, new MyCustomWidget(i));
    5. // Then, repopulate the layout from row 'i' downwards
    6. ...
    To copy to clipboard, switch view to plain text mode 

    where MyWidgetList is a QList<MyCustomWidget *>. In effect, there are three distinct operations here: firstly, create a new widget; secondly, insert its pointer into the QList; thirdly, add the new widget to the layout.

    However, I get into trouble when trying to remove a widget. In principle, I'd like to be able to do the above in reverse. Taking the widget out of the layout is easy, but I'm getting tied in knots over how to both remove the pointer from the QList AND destroy the widget itself. Currently, I use:

    Qt Code:
    1. // First, remove the widget from the layout:
    2. MyLayout -> removeWidget(MyWidgetList.at(i));
    3. // Now call the destructor:
    4. MyWidgetList.at(i) -> ~MyCustomWidget();
    5. // Now delete the pointer:
    6. MyWidgetList.removeAt(i);
    7. // Finally, refresh the layout once again...
    To copy to clipboard, switch view to plain text mode 

    When compiled with gcc, this seems to work, but then I seem to get random crashes after I've done this a couple of times, implying a memory leak. When compiled with MSVC, the program stops with an exception from deep within the heap management routines.

    There must be a 'proper' way to do this. The QList documenation provides the following example for clearing out all the widgets from a QList, namely:

    Qt Code:
    1. qDeleteAll(list.begin(), list.end());
    2. list.clear();
    To copy to clipboard, switch view to plain text mode 

    ...but there are no clues how one might go about clearing out a single entry.

    Can anyone advise?

  2. #2
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Remove widget from a QList

    why are you calling the destructor? just delete it.

    Qt Code:
    1. // Now call the destructor:
    2. delete(MyWidgetList[i]);
    To copy to clipboard, switch view to plain text mode 
    Last edited by jano_alex_es; 13th October 2009 at 09:20. Reason: spelling error

  3. The following user says thank you to jano_alex_es for this useful post:

    Eos Pengwern (14th October 2009)

  4. #3
    Join Date
    Aug 2009
    Posts
    50
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Remove widget from a QList

    You're right; that works!

    Classic beginner's error: getting so carried away with Qt's own methods that one overlooks the methods that standard C++ provides.

    Thank you very much.

  5. #4
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Remove widget from a QList

    You could also do this
    Qt Code:
    1. delete MyWidgetList.takeAt(i);
    To copy to clipboard, switch view to plain text mode 
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  6. The following user says thank you to ComaWhite for this useful post:

    Eos Pengwern (15th October 2009)

  7. #5
    Join Date
    Aug 2009
    Posts
    50
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Remove widget from a QList

    There's actually a postscript to this, which I'll record here for the benefit of anyone else who tries to do the same thing.

    Even after replacing my destructors with delete(), I still got heap errors when running in debug mode (and periodic crashes when outside the debugger). So, I resorted to Qt Support, and learned the real problem was this: among the various widgets that were being deleted was the 'delete' button on that row, whose 'clicked' signal was connected to the slot function that carries out the deleting.

    Now, Qt expects (quite reasonably) that when a signal is emitted and a slot function is called, it should be able to return afterwards to the object that emitted the signal. In this case, that object had been deleted by the end of the slot function, so there was nowhere for it to return to. Hence the heap error.

    Fortunately, the solution was very simple: just the addition of the Qt::QueuedConnection qualifier to the applicable signal/slot connection, i.e. changing

    Qt Code:
    1. connect(Delete_Buttons.at(i), SIGNAL(clicked(const int &)),
    2. this, SLOT(removeFilm(const int &)));
    To copy to clipboard, switch view to plain text mode 

    ...to:

    Qt Code:
    1. connect(Delete_Buttons.at(i), SIGNAL(clicked(const int &)),
    2. this, SLOT(removeFilm(const int &)), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 

    That was the only change required, and now it works perfectly.

Similar Threads

  1. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 07:06
  2. Replies: 1
    Last Post: 21st February 2009, 14:59
  3. Remove Widget Border?
    By winston2020 in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2008, 05:26
  4. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  5. Remove first n elements from QList
    By pakulo in forum Qt Programming
    Replies: 8
    Last Post: 4th June 2007, 07:27

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.