[Qt-interest] How to put a QLayout in a QScrollArea ?
Eric Clark
eclark at ara.com
Tue Dec 15 20:13:10 CET 2009
Stephen,
A QScrollArea already, by default, has at least two widgets in it, the vertical scroll bar and the horizontal scroll bar. It is not an “empty” widget. The two bars and the widget that it maintains (via setWidget) need to be is some kind of layout to lay those widgets out. If it did not have a layout, it would not be able to clip the widget with the scroll bars like it is intended to do. This all boils down to a basic understanding of how the layouts and widgets work together. I would suggest that you read the layout overview, found here http://doc.trolltech.com/4.6/layout.html. This should help you understand layouts and widgets and their relationship to each other a little better.
I would also like to point out that there is a mistake in the code that you sent and I am a little surprised that it works. Like I said in my previous emails, simply making the layout a child of a widget, sets the widget’s layout to the layout that was made a child of it. If there is a layout already there, it gets overridden. So, this code:
QWidget* widget = new QWidget(this);
vbox_ = new QVBoxLayout(this);
Overrides the layout for “this” which is the QScrollArea. Later in the constructor you call widget->setLayout(vbox_), I am going to guess that this is why the layout for the QScrollArea returns to the default. The above code, however, causes the QScrollArea’s layout to be overridden because you set the parent to be the QScrollArea. Instead, you should be making the parent of the layout, the widget. Like so (highlighted in red):
QWidget* widget = new QWidget(this);
vbox_ = new QVBoxLayout(widget);
Once you have set the parent of the layout to be the widget and not the scroll area, this automatically calls setLayout on the widget and there is no need for the extra call to setLayout at the end of your constructor. This is just something that I would like to point out so that you can maybe get a better understanding. Obviously, it works the way you have it, but I am very surprised that it does.
Thanks,
Eric
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Stephen Collyer
Sent: Tuesday, December 15, 2009 8:03 AM
Cc: Qt Interest (qt-interest at trolltech.com)
Subject: Re: [Qt-interest] How to put a QLayout in a QScrollArea ?
2009/12/14 Eric Clark <eclark at ara.com<mailto:eclark at ara.com>>
When you set the layout of the scroll area to be the VBoxLayout, you override the layout that controls the scroll bars in the QScrollArea. Do you not get a warning in your command console that says your QuestionSelectorList already has a layout?
No, I get nothing.
QWidget’s do not have layouts by default, but some subclasses, like QScrollArea, I believe, already does
That sounds odd. Why should an empty QScrollArea have a layout ?
Anyway, I've modified my original code based on your suggestion thus:
QuestionSelectorList::QuestionSelectorList(const QList<QuestionSelector *> qsels,
QWidget* parent)
: QScrollArea(parent)
{
QWidget* widget = new QWidget(this);
vbox_ = new QVBoxLayout(this);
foreach (QuestionSelector* qsel, qsels)
{
vbox_->addWidget(qsel);
}
widget->setLayout(vbox_);
setWidget(widget);
}
and this works fine. Thanks.
Would it be sacreligious of me to suggest that the Qt layout system is full of black magic and hidden secrets ?
--
Stephen Collyer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091215/618f728d/attachment.html
More information about the Qt-interest-old
mailing list