Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

layout->removeWidget(QWidget*) not working correctly



  • Hi to everyone,
    I'm developing an application which has different custom widgets. In particulate I have derived the QScrollArea widget for adding my own widgets to it. So far I can easily add widgets to it, but when it comes to deleting a widget it's kinda buggy, half of the widget disappear, but the remaining half is still visible and interactive. The following is my current code:

    void TasksList::removeTask(const unsigned short taskId) {
      auto children = _layout->children();
      for (auto i = 0; i < _layout->count(); i++) {
        TaskPreview* tmp = dynamic_cast<TaskPreview*>(_layout->itemAt(i)->widget());
        if (tmp && (tmp->getId() == taskId)) {
          _layout->removeWidget(tmp);
        }
      }
    }
    

    I should also point out that _layout is a pointer to the QScrollArea's associated widget layout.



  • void QLayout::removeWidget(QWidget *widget)
    Removes the widget widget from the layout. After this call, it is the caller's responsibility to give the widget a reasonable geometry or to put the widget back into a layout or to explicitly hide it if necessary.

    If you don't need the removed widget anymore, don't forget to delete it.



  • void QLayout::removeWidget(QWidget *widget)
    Removes the widget widget from the layout. After this call, it is the caller's responsibility to give the widget a reasonable geometry or to put the widget back into a layout or to explicitly hide it if necessary.

    If you don't need the removed widget anymore, don't forget to delete it.



  • Thank you, I actually commented out the delete because I wasn't sure of the removeWidget behaviour, now it works flawlessly