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] After removing widgets from layout with vertical spacer the spacer doesn't work anymore
Qt 6.11 is out! See what's new in the release blog

[Solved] After removing widgets from layout with vertical spacer the spacer doesn't work anymore

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 5.4k 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.
  • W Offline
    W Offline
    Woomla
    wrote on last edited by
    #1

    After clearing the content of a vertical layout it seems that the spacer that was there is also removed. At least, after the widgets in the layout are removed, and new widgets are added, the spacer doesn't seem to work.

    How can I remove the widgets and keep the verticalSpacer to work?

    This is how I remove the widgets from the layout.
    @
    QLayoutItem* child;
    while ((child = ui->layout->takeAt(0)) != 0)
    {
    QWidget* widget = child->widget();
    if (widget)
    {
    delete child->widget();
    delete child;
    }
    }
    @

    This is what I want: from startup, with vertical spacer, before clearing
    !http://i45.tinypic.com/10o32xh.jpg(With vertical spacer)!

    This is what I've got after clearing and adding two widgets, vertical spacer removed or not working?
    !http://i50.tinypic.com/35a0u36.jpg(Without vertical spacer)!

    Files:

    "lwmainwindow.cpp":http://pastebin.com/DPd1dfrK
    "lwmainwindow.h":http://pastebin.com/Dr8s7iBb
    "lwmainwindow.ui":http://pastebin.com/gCX8mwxH

    1 Reply Last reply
    0
    • B Offline
      B Offline
      b1gsnak3
      wrote on last edited by
      #2

      It is deleted as it is a child for the layout.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        b1gsnak3
        wrote on last edited by
        #3

        What you should add (I think) is a spacerItem for each clearing so that the widget only displays. And then when you add a new widget just insert a widget in between the spacer items and any previous widgets.

        1 Reply Last reply
        0
        • W Offline
          W Offline
          Woomla
          wrote on last edited by
          #4

          The strange thing is that it actually deletes 2 widges. Debugging shows no layout that's deleted, but still it's gone??

          I solved this by inserting a widget and a spacer in the layout. Then I insert/delete items to that widget, not to the layout. The widget grows/shrinks as items are added and deleted and the spacer makes sure that all items are pushed to the top.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            Your code removes all the QLayoutItems, including the QSpacerItem, from the QLayoutregardless of whether they contain a widget or not. QLayout::takeAt() removes an item from the layout. Whether you subsequently delete it or its contents is irrelevant: it is no longer part of the layout.

            1 Reply Last reply
            0
            • W Offline
              W Offline
              Woomla
              wrote on last edited by
              #6

              @ChrisW67: Never to old to learn something :p

              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