Results 1 to 7 of 7

Thread: Custom QLayout 'FlowLayout' in QScrollArea, can't update layout

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 Re: Custom QLayout 'FlowLayout' in QScrollArea, can't update layout

    Okay, I've gotten it working by going like this:
    Qt Code:
    1. void FlowLayout::insertWidgetAt(QWidget *widget, int index)
    2. {
    3. widget->setParent(this->parentWidget());
    4. this->itemList.insert(index, new QWidgetItem(widget));
    5. }
    To copy to clipboard, switch view to plain text mode 

    So all I needed to do was make sure the widget being added actually knows who its parent is.
    I suppose 'QLayout::addWidget()' normally does it, before passing the widget as a item to 'myCustomLayout::addItem()', but since I couldn't find the source for QLayout, I can't be sure.

    [Edit:] Found the QLayout source. Apparently the proper and safer call is 'QLayout::addChildWidget()':
    Qt Code:
    1. void FlowLayout::insertWidgetAt(QWidget *widget, int index)
    2. {
    3. QLayout::addChildWidget(widget);
    4. this->itemList.insert(index, new QWidgetItem(widget));
    5. }
    To copy to clipboard, switch view to plain text mode 
    This is what QLayout::addWidget() calls internally, and it checks for a number of different circumstances making the code more robust.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Custom QLayout 'FlowLayout' in QScrollArea, can't update layout

    Your code worked fine after moving the "setLayout" statement.

    snapshot1.png

  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: Custom QLayout 'FlowLayout' in QScrollArea, can't update layout

    Cool, but "QLayout::addChildWidget(widget)" is the proper way to do it, (or at least the way QLayout itself does it).
    It works fine this way, even after calls to setLayout(), which gives the programmer (me) more flexibility.

    It wouldn't be cool to have to call setLayout() after I insert the widgets... the whole point of 'inserting' the widgets, is to add widgets after the layout is already filled and created.

Similar Threads

  1. Replies: 6
    Last Post: 13th February 2011, 23:06
  2. Replies: 9
    Last Post: 3rd December 2010, 12:30
  3. Using QLayout for layout in a graphics scene
    By ticica in forum Qt Programming
    Replies: 1
    Last Post: 21st November 2009, 04:07
  4. Replies: 4
    Last Post: 5th September 2009, 15:24
  5. QLayout update size signal
    By bunjee in forum Qt Programming
    Replies: 3
    Last Post: 6th June 2008, 11:24

Tags for this Thread

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.