Results 1 to 4 of 4

Thread: QScrollArea confusion

  1. #1
    Join Date
    Jan 2014
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QScrollArea confusion

    Hi I'm trying to use a QScrollArea to manage the size of a certain widget.

    This widget is a list of smaller widgets (using a QVBoxLayout) which can be dynamically resized and which starts empty, and hence invisible.

    I can fully see and use the widget if I set it as the central widget of a QMainWindow. this means I can add widgets to the list, delete them etc.. and I can see everything.

    Then I decided I'd like some scroll bars to manage the size of the window. So I figured I could just create a QScrollArea, set the widget as the QScrollArea widget, and then set the QScrollArea as the central widget of the main window. I didn't do any configuation of size policies or anything thinking that I'd at least see something (anything!) first. The result is that I can't see anything. The widget is simply not visible.

    I have been reading the documention on size policies and what not on the QScrollArea page, but I have totally failed to understand this. I just can't understand why not setting some of these things have caused my widget to become totally invisible even when I add widgets to the list.

    Again, when I set this widget as the central widget of a QMainWindow, everything works as expected. The pseudo code is:

    Qt Code:
    1. mainWindow the_window;
    2. listWidget the_list;
    3. the_window.setCentralWidget(&the_list);
    To copy to clipboard, switch view to plain text mode 

    The above gives a working widget, without scroll bars

    Qt Code:
    1. mainWindow the_window;
    2. listWidget the_list;
    3. QScrollArea* the_area = new QScrollArea();
    4.  
    5. the_area->setWidget(&the_list);
    6. the_window.setCentralWidget(&the_list);
    To copy to clipboard, switch view to plain text mode 

    The above gives a window with nothing in it, no matter how many widgets I add to the list.

    If it's helpful, I'm using Qt 4.8.4 on ubuntu 13.10.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea confusion

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Container : public QWidget
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. explicit Container(QWidget *parent = 0) : QWidget(parent)
    9. {
    10. m_layout = new QVBoxLayout(this);
    11.  
    12. connect(&m_timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
    13. m_timer.start(1000);
    14. }
    15.  
    16. private:
    17. QTimer m_timer;
    18. QVBoxLayout *m_layout;
    19.  
    20. private slots:
    21. void onTimeout()
    22. {
    23. static int counter = 0;
    24. qDebug() << Q_FUNC_INFO << counter;
    25.  
    26. QLabel *label = new QLabel(QString::number(counter++));
    27. m_layout->addWidget(label);
    28. label->show();
    29. adjustSize();
    30. }
    31. };
    32.  
    33. int main(int argc, char** argv)
    34. {
    35. QApplication app(argc, argv);
    36.  
    37. QScrollArea scrollArea;
    38.  
    39. scrollArea.setWidget(new Container(&scrollArea));
    40.  
    41. scrollArea.show();
    42.  
    43. return app.exec();
    44. }
    45.  
    46. #include "scrollareatest.moc"
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Jan 2014
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QScrollArea confusion

    Thanks a lot for responding, The example was helpful, in that it encouraged me to try the 'adjustSize()' function.

    This finally caused the widgets within my list to be displayed.

    Basically after I add a widget to the list of widgets, I need to call the adjustSize() function to get the list widget to resize itself and become visible.

    However I'm still confused why the default behavior while set as the central widget for a QMainWindow didn't require me to call adjustSize(). It seems odd to me that the two should behave differently.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea confusion

    In the case of being a QMainWindow central widget, the container is part of a layout, the QMainWindow layout. In the QScrollArea case it is not and it is neither a top level window (which would resize itself).

    Cheers,
    _

Similar Threads

  1. Syntax confusion
    By baluk in forum Newbie
    Replies: 3
    Last Post: 11th December 2010, 16:58
  2. QScrollArea Confusion
    By TheJim01 in forum Newbie
    Replies: 3
    Last Post: 19th August 2010, 08:05
  3. Class Confusion
    By Petr_Kropotkin in forum Newbie
    Replies: 5
    Last Post: 23rd January 2010, 16:23
  4. Replies: 2
    Last Post: 10th March 2008, 21:16
  5. Qtopia Vs Qt/X11 confusion...
    By b1 in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 28th November 2007, 17:19

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.