Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved layout->removeWidget(QWidget*) not working correctly

    General and Desktop
    c++ qt 5.9.5 qlayout
    2
    3
    800
    Loading More Posts
    • 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.
    • D
      Daniel_Contro last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • B
        Bonnie last edited by

        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.

        1 Reply Last reply Reply Quote 2
        • B
          Bonnie last edited by

          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.

          1 Reply Last reply Reply Quote 2
          • D
            Daniel_Contro last edited by

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

            1 Reply Last reply Reply Quote 0
            • First post
              Last post