Results 1 to 3 of 3

Thread: Removing the last item in a layout or inserting a widget at the second-to-last spot?

  1. #1
    Join Date
    Apr 2009
    Location
    www.JaminGrey.com
    Posts
    71
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Removing the last item in a layout or inserting a widget at the second-to-last spot?

    I have a QScrollArea, and I add widgets to the scroll area as my program runs. I want the order of the widgets to be in the order I add them (first added should be at the top, second added should be next, and so on).

    I want all the widgets pushed to the top of the QScrollArea, so I call layout->stretch().

    The problem is, how can I insert a new widget at the second to last position of the QScrollArea, so it's after my previous widgets, but before the stretch?
    Alternatively, how can I delete the stretch, so I can add the widget and then re-add the stretch?

    I've tried messing with this numerous times in the past, and I always end up using some hacky way to get it working. What's the best way to do this?
    I'm aware of insertWidget, but I can't figure out how to get the index of the stretch so I can insert before it.

    Any hints?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Removing the last item in a layout or inserting a widget at the second-to-last sp

    Qt Code:
    1. QWidget *newWidget = ...;
    2.  
    3. layout->insertWidget(layout->count() - 1, newWidget);
    To copy to clipboard, switch view to plain text mode 
    seems the obvious approach.

    You can probably do away with the spacer item (I assume) altogether using the layout's size constraint.

  3. #3
    Join Date
    Apr 2009
    Location
    www.JaminGrey.com
    Posts
    71
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Removing the last item in a layout or inserting a widget at the second-to-last sp

    Hey, thanks alot. I've checked for a layout->count() type function and missed it. I was looking for a size(), I've been working with the C++ standard library too long. =)
    I also thought of widget->children().count(), but the parent might have non-layout children, and the layout itself doesn't own the children it has.

    If I altered the layout's size constraint, and only have, say, 3 objects in the layout, if those widgets are less than the QScrollArea size (fixed minimum size), the QScrollArea would make the widget hover in the center, instead of at the top... or so I assumed, but another perusal of the documentations says otherwise (QScrollAreas have alignments, which I forgot about, plus it defaults to top-left and not the center).

Similar Threads

  1. Removing item from layout without deleting it.
    By mrc_pl in forum Qt Programming
    Replies: 2
    Last Post: 24th January 2012, 11:24
  2. Removing widget from layout
    By lotek in forum Newbie
    Replies: 13
    Last Post: 21st July 2011, 04:40
  3. Replies: 1
    Last Post: 10th May 2011, 22:35
  4. old layout item remain on widget
    By lllturtle in forum Qt Programming
    Replies: 4
    Last Post: 28th March 2011, 02:07
  5. Inserting progress bar as an item
    By kremuwa in forum Qt Programming
    Replies: 2
    Last Post: 5th April 2010, 22:15

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.