Results 1 to 4 of 4

Thread: QScrollArea Confusion

  1. #1
    Join Date
    Feb 2010
    Location
    Pennsylvania, USA
    Posts
    36
    Thanks
    9
    Thanked 3 Times in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QScrollArea Confusion

    I'm trying to learn to use a QScrollArea, but I'm obviously missing some important step.

    Using Qt Creator (or designer, it doesn't matter), I create a QMainWindow and add a QScrollArea. I then drop a standard "Widget" into the QScrollArea (and set its style sheet to "background: red;" just so I can see it), and add a standard push button.

    I add the following slot to the button:
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. ui->widget->resize(5000, ui->widget->height());
    4. }
    To copy to clipboard, switch view to plain text mode 
    (The rest of the code is generated by Creator, but I can provide it if requested.)

    My anticipated behavior is: when I click the button, it will re-size the child widget, thereby causing the scroll area to adjust its scroll bars--in this case only the horizontal bar. Unfortunately, that doesn't happen. I can tell that clicking the button does re-size the child widget, but the scroll area's scroll bars do not update--no handles appear, and the entire scroll bar remains gray, as if disabled.

    I have tried enabling the scroll bar, with no effect. I ensured widgetResizable is set to false, but that didn't effect the result either. I originally thought a layout I was using for the child widget was causing the problem, but removing it didn't change the result. The way I understand (or maybe misunderstand) the documentation, by default a QScrollArea should respect the size of its child, and adjust the scroll bars automatically. What did I miss?
    Last edited by TheJim01; 24th March 2010 at 15:23.

  2. #2
    Join Date
    Feb 2010
    Location
    Pennsylvania, USA
    Posts
    36
    Thanks
    9
    Thanked 3 Times in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea Confusion

    Still not fixed, but I've come a little further. Apparently there were conflicting definitions of whether widgetResizable was true or not. I am now 100% sure it is set to false (I checked with a QMessageBox in my clicked() function).

    Now, the scroll bar looks active, and there is a large handle in it, regardless of the size of the child widget. When the form is first loaded, the child widget is much smaller than the scroll area, but the big bar is still there. When I click the button, which makes the child widget much wider than the scroll area, the big bar is still there, unchanged.

    I'm trying, but I'm obviously still missing some critical step. Any ideas?

  3. #3
    Join Date
    Feb 2010
    Location
    Pennsylvania, USA
    Posts
    36
    Thanks
    9
    Thanked 3 Times in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea Confusion

    I think I figured it out. Creator did something strange in ui_mainwindow.h:
    Qt Code:
    1. QWidget *centralWidget;
    2. QVBoxLayout *verticalLayout;
    3. QScrollArea *scrollArea;
    4. QWidget *scrollAreaWidgetContents;
    5. QWidget *widget;
    6. QHBoxLayout *horizontalLayout;
    7. QSpacerItem *horizontalSpacer;
    8. QLabel *label;
    9. QLineEdit *widthVal;
    10. QPushButton *pushButton;
    11. QSpacerItem *horizontalSpacer_2;
    12. QSpacerItem *verticalSpacer;
    13. QMenuBar *menuBar;
    14. QToolBar *mainToolBar;
    15. QStatusBar *statusBar;
    16.  
    17. void setupUi(QMainWindow *MainWindow)
    18. {
    19. if (MainWindow->objectName().isEmpty())
    20. MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    21. MainWindow->resize(407, 205);
    22. centralWidget = new QWidget(MainWindow);
    23. centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
    24. verticalLayout = new QVBoxLayout(centralWidget);
    25. verticalLayout->setSpacing(6);
    26. verticalLayout->setContentsMargins(11, 11, 11, 11);
    27. verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    28. scrollArea = new QScrollArea(centralWidget);
    29. scrollArea->setObjectName(QString::fromUtf8("scrollArea"));
    30. QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    31. sizePolicy.setHorizontalStretch(0);
    32. sizePolicy.setVerticalStretch(0);
    33. sizePolicy.setHeightForWidth(scrollArea->sizePolicy().hasHeightForWidth());
    34. scrollArea->setSizePolicy(sizePolicy);
    35. scrollArea->setMinimumSize(QSize(0, 100));
    36. scrollArea->setMaximumSize(QSize(16777215, 100));
    37. scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    38. scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    39. scrollAreaWidgetContents = new QWidget();
    40. scrollAreaWidgetContents->setObjectName(QString::fromUtf8("scrollAreaWidgetContents"));
    41. scrollAreaWidgetContents->setGeometry(QRect(0, 0, 387, 82));
    42. widget = new QWidget(scrollAreaWidgetContents);
    43. widget->setObjectName(QString::fromUtf8("widget"));
    44. widget->setGeometry(QRect(9, 9, 41, 31));
    45. widget->setStyleSheet(QString::fromUtf8("background:red;"));
    46. scrollArea->setWidget(scrollAreaWidgetContents);
    47.  
    48. ...
    To copy to clipboard, switch view to plain text mode 
    Notice the QWidget pointer "scrollAreaWidgetContents". That becomes the scroll area's child. When my red widget ("widget") was created, "scrollAreaWidgetContents" was passed as its parent. Normally, I would think that "scrollAreaWidgetContents" would act as a pass-through container, since the QScrollArea object doesn't provide a way of defining its child widget through its Creator properties.

    Oh well, I'll just have to remember that if I change the UI in the Designer, that I have to change the last line in the code above to
    Qt Code:
    1. scrollArea->setWidget(widget);
    To copy to clipboard, switch view to plain text mode 

    The more I use Creator, the more I see its shortcomings, and the power of hand-coding. Even for fast prototyping (what I'm doing now), I see it's not all that it could be.

  4. #4
    Join Date
    Aug 2010
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea Confusion

    [This reply is probably too late for you, but I've seen the same question in a few places, and the doc does not spell it out, so this may benefit others who come looking for an answer. ]

    I think you'll find this will achieve the same result.
    i.e. right-click on scrollArea : Layout : Lay out Horizontally (or vertically ..)
    When scrollArea is created, it is given a scrollAreaWidgetContents, but it has a 'no layout' icon (like the Layout : Break Layout icon).
    When scrollAreaWidgetContents has no child, the Layout cannot be set (greyed-out options in Layout menu).

    The doc on QScrollArea has a section on 'Size Hints and Layouts', but I think Qt Designer needs some additional doc that makes this requirement clear.

    I was able to prototype this behaviour in Qt Designer, and confirm that this change caused resizing and scrollbars as required.

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

    Miss Engineer (2nd June 2014)

Similar Threads

  1. confusion city
    By Petr_Kropotkin in forum Newbie
    Replies: 1
    Last Post: 3rd February 2010, 16:30
  2. Class Confusion
    By Petr_Kropotkin in forum Newbie
    Replies: 5
    Last Post: 23rd January 2010, 16:23
  3. QwtPlotMarker confusion
    By baray98 in forum Qwt
    Replies: 3
    Last Post: 20th July 2008, 10:47
  4. Replies: 2
    Last Post: 10th March 2008, 21:16
  5. QLocale confusion :(
    By gri in forum Qt Programming
    Replies: 6
    Last Post: 15th June 2007, 14:09

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.