[Qt-interest] Poorly behaving QScrollArea
Donovan Parks
donovan.parks at gmail.com
Mon Feb 8 04:51:47 CET 2010
Hello,
I am new to Qt and am having a rather difficult time understanding how
to correctly use a QScrollArea. I'm trying to create a simple
application where there is a QScrollArea on the left of the screen and
a QFrame on the right. The contents (i.e., children widgets) of the
QScrollArea are added dynamically during program execution. I'd like
to have this QScrollArea automatically resize so it always fully shows
the horizontal extents of the contained children widgets, but may
require a scrollbar to show the vertical extents. Unfortunately, I am
having trouble getting the QScrollArea to resize when I add chidlren
widgets to it. There is plenty of room on the screen for the
QScrollArea to fill (i.e., the QFrame on the right has a size policy
of QSizePolicy::Expanding, but requires much less space).
My QScrollArea is providing a view into a QWidget named
scrollAreaWidgetContents. A QVBoxLayout is added to this widget that
contains all the children widgets. I've set the size policy of this
layout to QLayout::SetMinAndMaxSize as per the documentation on
QScrollArea. My understanding is that this would cause the size of
scrollAreaWidgetContents to fit the required minimum size of the
QVBoxLayout. However, this does not appear to be happening.
Some (not all) of the relevant code is below. You can find a picture
of the problem at
http://dparks.wikidot.com/local--files/file-dump/BadQScrollArea.png.
The complete source (along with a VS project file) can be found at
http://dparks.wikidot.com/local--files/file-dump/QtBadQScrollArea.zip.
Any and all help is much appreciate. I've been stuck on this one for
the last few days and am out of avenues to explore.
QtSandbox::QtSandbox(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
QObject::connect(ui.btnAddButtonToScrollArea, SIGNAL(clicked()),
this, SLOT(addButtonToScrollArea()));
// This is the QScrollArea widget. Set the sizing policy so it
follows the minimum required size of its children.
QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(ui.scrollArea->sizePolicy().hasHeightForWidth());
ui.scrollArea->setSizePolicy(sizePolicy);
ui.scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui.scrollArea->setWidgetResizable(true);
// This is the widget that the QScrollArea is providing a view
of. Again, set its size policy to reflect its children.
QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Preferred);
sizePolicy1.setHorizontalStretch(0);
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(ui.scrollAreaWidgetContents->sizePolicy().hasHeightForWidth());
ui.scrollAreaWidgetContents->setSizePolicy(sizePolicy1);
ui.scrollArea->setWidget(ui.scrollAreaWidgetContents);
}
void QtSandbox::addButtonToScrollArea()
{
QVBoxLayout *vlay = new QVBoxLayout();
vlay->setSizeConstraint(QLayout::SetMinAndMaxSize);
QPushButton* button = new QPushButton("This is a really long button.", this);
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
button->setSizePolicy(sizePolicy);
vlay->addWidget(button);
delete ui.scrollAreaWidgetContents->layout();
ui.scrollAreaWidgetContents->setLayout(vlay);
// I would have thought at this point the widget would expand
to the required minimum size of the widgets in vlay layout.
ui.scrollAreaWidgetContents->updateGeometry();
}
Cheers,
Donovan
More information about the Qt-interest-old
mailing list