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. [solved] How to properly delete a QLayoutItem from the layout
Forum Updated to NodeBB v4.3 + New Features

[solved] How to properly delete a QLayoutItem from the layout

Scheduled Pinned Locked Moved General and Desktop
qlayoutqwidgetqlayoutitemdelete
5 Posts 3 Posters 4.9k Views 1 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    Hi there!
    I'm trying to delete a Widget out of a layout. The widget can be at an arbitrary (but existing) position and lower items have to move up to close the gap. layout_m is of type QLayout,. Here's my delete member function:
    if(index >= layout_m->count() || index < 0) return;
    QLayoutItem* toDelete = layout_m->takeAt(index);
    if(toDelete != 0){
    toDelete->widget()->setParent(NULL);
    delete toDelete;
    requestedCount_m--;
    }
    This code is inspired from the QLayout documentation's description how to properly delete all contents of a layout. However, as you see, I use toDelete->widget()->setParent(NULL); to break ownership to the QLayoutItem. If I don't do that, the deleted item is still displayed in the parent widget, but not in the layout.
    I conclude that my way to remove an item from a layout is unproper. How should I do that in a clean way?
    Cheers,
    Kalsan

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tarod.net
      wrote on last edited by tarod.net
      #2

      @kalsan As far as I know, QLayout::takeAt() doesn't remove the widget of the QLayoutItem from its parent widget.

      So I think your code is ok. Anyway, take a look at QLayout::removeWidget. I don't know all your code, but maybe you should remove the widget from the layout using that method.

      "Individually, we are one drop. Together, we are an ocean."

      1 Reply Last reply
      0
      • M Offline
        M Offline
        msue
        wrote on last edited by
        #3

        I just call:

        widgetToRemoveFromLayout.close();
        delete widgetToRemoveFromLayout;
        

        everything else does the layout on its own.

        T 1 Reply Last reply
        0
        • M msue

          I just call:

          widgetToRemoveFromLayout.close();
          delete widgetToRemoveFromLayout;
          

          everything else does the layout on its own.

          T Offline
          T Offline
          tarod.net
          wrote on last edited by
          #4

          @msue Right then :)

          "Individually, we are one drop. Together, we are an ocean."

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Yay, that's what I was looking for. Thanks!

            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