Qt-interest Archive, April 2007
Layout in QScrollArea
Message 1 in thread
Hi,
I'm trying to get a QVBoxLayout to work inside a QScrollArea in Qt 4.2.2 and
I can't get it to do what I want.
When I call layout->addWidget() it either squeezes my widgets smaller and
smaller with each added widget, approaching 0 height for each, or if I set
the minimum height for my widgets, they overlap.
The behaviour I want is for the layout inside the QScrollArea to expand, and
for the vertical scrollbar to appear.
How can I get it to work the way I want? I'm sure I must just be missing
something simple.
I have attached a simple example of what I'm doing. In the real program,
widgets are added/removed from the layout when buttons are clicked.
Thanks for any hints!
--
[ signature omitted ]
Message 2 in thread
> I'm trying to get a QVBoxLayout to work inside a QScrollArea in Qt 4.2.2 and
> I can't get it to do what I want.
>
> When I call layout->addWidget() it either squeezes my widgets smaller and
> smaller with each added widget, approaching 0 height for each, or if I set
> the minimum height for my widgets, they overlap.
>
> The behaviour I want is for the layout inside the QScrollArea to expand, and
> for the vertical scrollbar to appear.
Hi
Sounds like the layout is set on the scroll area (or it's viewport).
What you should do is to create a new plain QWidget, install the
layout on it, and set it inside the scroll area using
QScrollArea::setWidget().
--
[ signature omitted ]
Message 3 in thread
"J-P Nurmi" <jpnurmi@xxxxxxxxx> wrote in message
news:34cc19920704242310w41ea91fcj91d48b7ce27576dc@xxxxxxxxxxxxxxxxx
> > The behaviour I want is for the layout inside the QScrollArea to expand,
> > and for the vertical scrollbar to appear.
>
> Sounds like the layout is set on the scroll area (or it's viewport).
> What you should do is to create a new plain QWidget, install the
> layout on it, and set it inside the scroll area using
> QScrollArea::setWidget().
Thanks for the response.
I still can't get this to work. If I call QScrollArea::setWidget() before
the child widgets are added, nothing appears in the scroll area. If I call
setWidget() after I do everything else, the widgets appear but they are set
to their minimum size instead of filling the scroll area. The scrollbars do
appear if the minimum size of the child widgets is large enough.
It seems like there's a chicken & egg sizing problem here.
Also, what I want is to be able to add and remove widgets from the
QScrollArea as needed, not set it all up first.
--
[ signature omitted ]
Message 4 in thread
On 4/25/07, Doug Spence <doug.spence@xxxxxxxxxx> wrote:
>
> I still can't get this to work. If I call QScrollArea::setWidget() before
> the child widgets are added, nothing appears in the scroll area. If I
> call
> setWidget() after I do everything else, the widgets appear but they are
> set
> to their minimum size instead of filling the scroll area. The scrollbars
> do
> appear if the minimum size of the child widgets is large enough.
>
> It seems like there's a chicken & egg sizing problem here.
I recall having a similar problem, and I believe moving the call to
QScrollArea::setWidget() to after I had added everything that I wanted to be
scrollable "fixed" the problem. If that works for you, it may give you a
good starting place for really fixing the problem (or for filing a bug
report with Trolltech).
Also, what I want is to be able to add and remove widgets from the
> QScrollArea as needed, not set it all up first.
I agree that this should be easy, but for some reason I haven't found a
simple way of doing it.
Tom
Message 5 in thread
"Tom Panning" <lurchvt@xxxxxxxxx> wrote in message
news:1dc2e45d0704250721r5ff741caxf7217987f76011e@xxxxxxxxxxxxxxxxx
> On 4/25/07, Doug Spence <doug.spence@xxxxxxxxxx> wrote:
> >
> > I still can't get this to work. If I call QScrollArea::setWidget()
> > before the child widgets are added, nothing appears in the scroll
> > area. If I call setWidget() after I do everything else, the
> > widgets appear but they are set to their minimum size instead of
> > filling the scroll area. The scrollbars do appear if the minimum
> > size of the child widgets is large enough.
> >
> > It seems like there's a chicken & egg sizing problem here.
>
>
> I recall having a similar problem, and I believe moving the call to
> QScrollArea::setWidget() to after I had added everything that I wanted to
be
> scrollable "fixed" the problem. If that works for you, it may give you a
> good starting place for really fixing the problem (or for filing a bug
> report with Trolltech).
Even that doesn't work the way I'd like.
> > Also, what I want is to be able to add and remove widgets from the
> > QScrollArea as needed, not set it all up first.
>
>
> I agree that this should be easy, but for some reason I haven't found a
> simple way of doing it.
For the moment, I have added the QWidget that J-P Nurmi suggested, with the
layout on that. Then when I add or remove child widgets, I call
QWidget::adjustSize() on them. But I even this doesn't work if I do it
immediately (the widgets overlap). I push pointers to the widgets into a
queue and fire a single shot timer that executes a slot that calls
adjustSize() on everything in the queue.
Even with that, the child widgets don't adjust to the width of the
QScrollArea they're in. They keep the width from their minimumSize setting,
no matter which SizePolicy is used.
So now I've got some fixed size stuff that ruins the whole layout idea.
I guess I'll e-mail Trolltech directly to see what they suggest. Hopefully
there's something obvious and simple that I missed.
--
[ signature omitted ]
Message 6 in thread
> For the moment, I have added the QWidget that J-P Nurmi suggested, with the
> layout on that. Then when I add or remove child widgets, I call
> QWidget::adjustSize() on them. But I even this doesn't work if I do it
> immediately (the widgets overlap). I push pointers to the widgets into a
> queue and fire a single shot timer that executes a slot that calls
> adjustSize() on everything in the queue.
>
> Even with that, the child widgets don't adjust to the width of the
> QScrollArea they're in. They keep the width from their minimumSize setting,
> no matter which SizePolicy is used.
Try QScrollArea::setWidgetResizable(true).
--
[ signature omitted ]
#include <QtGui>
static const int COLS = 4;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent = 0)
: QMainWindow(parent)
{
QScrollArea* scroll = new QScrollArea(this);
setCentralWidget(scroll);
QWidget* container = new QWidget(this);
grid = new QGridLayout(container);
scroll->setWidgetResizable(true);
scroll->setWidget(container);
QMenu* menu = menuBar()->addMenu("Menu");
menu->addAction("Add row", this, SLOT(addRow()));
menu->addSeparator();
menu->addAction("Quit", this, SLOT(close()));
}
private slots:
void addRow()
{
int row = grid->rowCount();
for (int col = 0; col < COLS; ++col)
{
QPushButton* btn = new QPushButton(QString("(%1,%2)").arg(row).arg(col));
grid->addWidget(btn, row, col);
}
}
private:
QGridLayout* grid;
};
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "main.moc"
Message 7 in thread
"J-P Nurmi" <jpnurmi@xxxxxxxxx> wrote in message
news:34cc19920704260351j15203325l76d6700f5246a4a1@xxxxxxxxxxxxxxxxx
> > For the moment, I have added the QWidget that J-P Nurmi suggested, with
the
> > layout on that. Then when I add or remove child widgets, I call
> > QWidget::adjustSize() on them. But I even this doesn't work if I do it
> > immediately (the widgets overlap). I push pointers to the widgets into
a
> > queue and fire a single shot timer that executes a slot that calls
> > adjustSize() on everything in the queue.
> >
> > Even with that, the child widgets don't adjust to the width of the
> > QScrollArea they're in. They keep the width from their minimumSize
setting,
> > no matter which SizePolicy is used.
>
> Try QScrollArea::setWidgetResizable(true).
That's the "something obvious and simple that I missed"! Thank you! That
works perfectly and I don't need the other stuff with the queue and
adjustSize(). :-)
--
[ signature omitted ]
Message 8 in thread
> Try QScrollArea::setWidgetResizable(true).
>
> --
> J-P Nurmi
oh my! thank you!
i was struggling with a similar problem as the OP, and this solved it!
Cheers,
Peter
--
[ signature omitted ]