Results 1 to 4 of 4

Thread: dynamic qscrollarea

  1. #1

    Default dynamic qscrollarea

    I have a freeflow layout inside a scrollarea that when new labels get added to that freeflow layout the scrollarea does not work. The scrollbar never changes even if I resize the form.

    Here is some code

    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "flowlayout.h"
    4. #include "window.h"
    5. #include "dropsitewidget.h"
    6.  
    7. Window::Window()
    8. {
    9.  
    10. // setting up the debig textbox
    11. this->log = new QTextEdit;
    12.  
    13.  
    14. // setting up the flow layout
    15. this->flowLayout = new FlowLayout;
    16. this->flowLayout->setSizeConstraint(QLayout::SetMinimumSize);
    17.  
    18. QScrollArea *scrollArea = new QScrollArea;
    19. scrollArea->setLayout(this->flowLayout);
    20.  
    21. // adding the 20 labels for images
    22. for (int i=0; i<20; i++) {
    23. //this->flowLayout->addWidget(new QLabel(tr("its a label")));
    24.  
    25. QLabel *label = new QLabel("image here");
    26. label->setFixedSize(100,100);
    27.  
    28. this->labelList.append(label);
    29. this->flowLayout->addWidget(this->labelList.at(i));
    30. }
    31.  
    32. // adding the widget to do the drag and drop
    33. dropSiteWidget = new DropSiteWidget;
    34. connect(dropSiteWidget, SIGNAL(sendLog(QString)), log, SLOT(append(QString)));
    35. connect(dropSiteWidget, SIGNAL(sendImage(QString)), this, SLOT(newImage(QString)));
    36.  
    37.  
    38.  
    39. QVBoxLayout *layout = new QVBoxLayout;
    40. layout->addWidget(dropSiteWidget);
    41. layout->addWidget(scrollArea);
    42. layout->addWidget(log);
    43. setLayout(layout);
    44.  
    45. this->count = 0;
    46.  
    47. setWindowTitle(tr("Flow Layout"));
    48. }
    49.  
    50. void Window::newImage(QString str) {
    51. // loading the new image
    52. QPixmap *img = new QPixmap(str);
    53.  
    54. // checking if its a valid image
    55. if (!img->isNull()) {
    56. // scaling the image now
    57. QPixmap smallImg;
    58. smallImg = img->scaledToWidth(100);
    59.  
    60. this->labelList.at(this->count)->setPixmap(smallImg);
    61. this->count++;
    62. }
    63. }
    To copy to clipboard, switch view to plain text mode 


    freeflow and dragdropwidget are taken from the examples

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: dynamic qscrollarea

    Try using QScrollArea::setWidget() instead of this:
    Qt Code:
    1. scrollArea->setLayout(this->flowLayout);
    To copy to clipboard, switch view to plain text mode 
    Just wrap the flow layout in a widget and set it into the scroll area by using the method mentioned above.
    J-P Nurmi

  3. #3

    Default Re: dynamic qscrollarea

    I have tried that too.. but the widget does not scale.. i guess i'd need to subclass it to scale correctly?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: dynamic qscrollarea

    Ok, I see the problem. A scroll area does not resize it's widget by default. Therefore a wrapper widget (containing the flow layout) inside the scroll area can have as much space as it wants and the labels don't get laid like you want. You can make the QScrollArea to resize it's widget by setting QScrollArea::widgetResizable property as true. This is not enough for a proper solution, though. This way the labels would get laid correctly, but no scroll bars appear before there is less available space than a one single label requires. This is because FlowLayout::sizeHint() returns only the size of the biggest label. So clearly the FlowLayout needs some patching..

    At first, you need a wrapper widget as we were discussing earlier. You could set the scroll area up more or less like this:
    Qt Code:
    1. QScrollArea* scroll = new QScrollArea(this);
    2. QWidget* wrapper = new QWidget(scroll);
    3. FlowLayout* flow = new FlowLayout(wrapper);
    4. for (int i = 0; i < 20; ++i)
    5. {
    6. QLabel* label = new QLabel("image here", wrapper);
    7. label->setFixedSize(100, 100);
    8. flow->addWidget(label);
    9. }
    10. wrapper->setLayout(flow);
    11. scroll->setWidget(wrapper);
    12. scroll->setWidgetResizable(true);
    To copy to clipboard, switch view to plain text mode 

    Then, you'll need to modify the FlowLayout to get the desirable behaviour. Make FlowLayout::minimumSize() look like this:
    Qt Code:
    1. QSize FlowLayout::minimumSize() const
    2. {
    3. int w = geometry().width();
    4. int h = doLayout(QRect(0, 0, w, 0), true);
    5. return QSize(w, h);
    6. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    vycke (25th April 2008)

Similar Threads

  1. Replies: 7
    Last Post: 20th September 2006, 15:45
  2. is there any signal for QScrollArea?
    By rajesh in forum Qt Programming
    Replies: 1
    Last Post: 21st July 2006, 08:12
  3. Using QScrollArea
    By Jimmy2775 in forum Qt Programming
    Replies: 4
    Last Post: 2nd March 2006, 22:26
  4. QScrollArea problem positioning a QWidget inside
    By Spectator in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2006, 23:59
  5. Problem with QScrollArea updating from 4.0.1 to 4.1.0
    By SkripT in forum Qt Programming
    Replies: 8
    Last Post: 28th January 2006, 23:35

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.