[Qt-interest] How to put a QLayout in a QScrollArea ?
Eric Clark
eclark at ara.com
Mon Dec 14 20:44:28 CET 2009
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?
You need to create a new QWidget and make the QVBoxLayout a child of that widget to set its layout, you don’t even need to call setLayout as long as you make the layout a child of the widget. Then set the widget on the QScrollArea to be the newly created widget with the newly created layout. QWidget’s do not have layouts by default, but some subclasses, like QScrollArea, I believe, already does. Usually Qt spits out a warning when you try to set the layout of a widget that already has one.
Example:
// Create a widget with no layout
QWidget *widget = new QWidget(myScrollArea);
// Add the layout to the widget
QVBoxLayout *layout = new QVBoxLayout(widget);
// Set the widget in your scroll area to the widget with the layout
myScrollArea->setWidget(widget);
Thanks,
Eric
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Stephen Collyer
Sent: Monday, December 14, 2009 1:25 PM
Cc: qt-interest at trolltech.com
Subject: Re: [Qt-interest] How to put a QLayout in a QScrollArea ?
2009/12/14 Sean Harmer <sean.harmer at maps-technology.com<mailto:sean.harmer at maps-technology.com>>
QVBoxLayout* layout = new QVBoxLayout;
// Add widgets to layout...
QWidget* w = new QWidget;
w->setLayout( layout );
myScrollArea->setWidget( w );
Well, I've tried a variant of this:
class QuestionSelectorList : public QScrollArea
{
Q_OBJECT
public:
QuestionSelectorList(const QList<QuestionSelector *> qsels,
QWidget* parent = 0);
~QuestionSelectorList();
private:
QVBoxLayout* vbox_;
};
QuestionSelectorList::QuestionSelectorList(const QList<QuestionSelector *> qsels,
QWidget* parent)
: QScrollArea(parent)
{
vbox_ = new QVBoxLayout(this);
foreach (QuestionSelector* qsel, qsels)
{
vbox_->addWidget(qsel);
}
setLayout(vbox_);
}
and this doesn't work properly. It fails to show any QuestionSelector widgets initially but if resized, it reveals more and more of them. It's as if the QScrollArea is failing to get a meaningful size for vbox_ for some reason. (It never displays a scroll bar either).
Any ideas ?
--
Stephen Collyer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091214/e446d9c1/attachment.html
More information about the Qt-interest-old
mailing list