Results 1 to 7 of 7

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

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

    I am using the FlowLayout custom layout example, with one small modification, inside a QScrollLayout.

    The custom modification is that I'm adding a 'insertWidgetAt' function (alongside the normal 'addWidget' function), but try as I might, I can't seem to get the inserted widget to display.
    Qt Code:
    1. void FlowLayout::insertWidgetAt(QWidget *widget, int index)
    2. {
    3. this->itemList.insert(index, new QWidgetItem(widget));
    4.  
    5. //What function do I need to call here, to make the inserted widget show up?
    6. }
    To copy to clipboard, switch view to plain text mode 

    I've tried:
    this->invalidate();
    this->update();
    this->activate();
    this->parentWidget()->updateGeometry();
    this->parentWidget()->update();


    And: (from the Qt FlowLayout example)
    doLayout(QLayout::geometry(), false);
    doLayout(QRect(0, 0, 0, 0), false);


    Calling the regular customLayout->addWidget() works fine. The customLayout->insertWidgetAt() never shows up, even if I call addWidget() afterward.

    My guess at the problem: I never inform the QScrollArea of the widget... only the QScrollArea's layout. Does the layout somehow inform the parent widget of added widgets? How is my custom layout supposed to do that?

    QLayout::addWidget() calls the FlowLayout::addItem(). Clearly something happens in QLayout::addWidget() that is significant, that I'm not aware of. I've looked in the Qt source, but I can't find the QLayout source file.
    I've also looked at QBoxLayout::insertWidgetAt() source code, and it doesn't seem to be doing anything special, though I can't be sure because of the heavy use of unfamiliar macros.

  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

    Works fine here:

    flowcontrol.cpp
    Qt Code:
    1. flowLayout->insertWidgetAt(new QPushButton(tr("INSERTED ITEM")),2);
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. Window window;
    2. sa->setWidget(&window);
    3. sa->setMaximumHeight(100);
    4. sa->show();
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  3. The following user says thank you to norobro for this useful post:

    ComServant (13th August 2011)

  4. #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

    Wierd. I just knocked together a minimalistic example, and it's still not working.

    main.cpp: http://pastebin.com/hABZSjxj
    FlowLayout.h: http://pastebin.com/fB4yi4sB
    FlowLayout.cpp: http://pastebin.com/iVwEzjJa

    Specifically, insertWidgetAt: (from FlowWidget.cpp)
    Qt Code:
    1. void FlowLayout::insertWidgetAt(QWidget *widget, int index)
    2. {
    3. this->itemList.insert(index, new QWidgetItem(widget));
    4. }
    To copy to clipboard, switch view to plain text mode 

    Am I missing something?

    Where I inserted another QPushButton, it creates a bulge, but doesn't show the inserted button:


    Could you post your example, so I can compare it to mine?

  5. #4
    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

    Try this:
    Qt Code:
    1. // frame->setLayout(layout);
    2. layout->addWidget(new QPushButton("One"));
    3. layout->addWidget(new QPushButton("Two"));
    4. layout->addWidget(new QPushButton("Three"));
    5. layout->addWidget(new QPushButton("Four"));
    6. layout->insertWidgetAt(new QPushButton("2.5"), 2);
    7. frame->setLayout(layout);
    To copy to clipboard, switch view to plain text mode 

  6. #5
    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.

  7. #6
    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

  8. #7
    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: 14th February 2011, 00:06
  2. Replies: 9
    Last Post: 3rd December 2010, 13:30
  3. Using QLayout for layout in a graphics scene
    By ticica in forum Qt Programming
    Replies: 1
    Last Post: 21st November 2009, 05:07
  4. Replies: 4
    Last Post: 5th September 2009, 16:24
  5. QLayout update size signal
    By bunjee in forum Qt Programming
    Replies: 3
    Last Post: 6th June 2008, 12: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.