PDA

View Full Version : Dynamic updates of a QWidget in a QScrollArea



plamkata
20th July 2008, 21:37
Hi,

I have a problem with a scroll area for a QWidget when contents get
changed dynamically, e.g,., added to that QWidget after the setWidget
call for the QScrollArea. Instead of a lengthy explanation, here is a
simple example:



--- Example.cpp ---
#include <QApplication>
#include <QWidget>
#include <QGridLayout>
#include <QPushButton>
#include <QScrollArea>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *test = new QWidget;
QScrollArea area(0);
area.setWidget(test); // A
QGridLayout gl(test);
gl.setSpacing(0);
gl.setMargin(0);
QPushButton b0("1", test);
gl.addWidget(&b0,0,0);
QPushButton b1("2", test);
gl.addWidget(&b1,0,1);
QPushButton b2("3", test);
gl.addWidget(&b2,1,0);
QPushButton b3("4", test);
gl.addWidget(&b3,1,1);
QPushButton b4("5", test);
gl.addWidget(&b4,2,0);
QPushButton b5("6", test);
gl.addWidget(&b5,2,1);
// area.setWidget(test); // B
area.show();
return app.exec();
}
--- Example.cpp ---


If I first call setWidget() to put the widget into the QScrollArea and
then add sub-widgets, e.g., QPushButtons then the QWidget displayed in
the QScroll area is empty. Everything works fine if I comment out the
setWidget() call marked "A" and call setWidget() at location"B". However,
I do have a set up where I want to make changes to the QWidget based
on user interaction where it is no longer an option to do the
setWidget() call after changes. Is there a proper way to notify the
QScrollArea about the fact that the QWidget it contains has changed?

Thank you for any help

kghose
20th July 2008, 23:07
I'm using a qgraphicspixmap in a qgraphicsview and any changes to the pixmap showup when update is called.
What I would imagine you should do is call update() when you change the QWidget
-K

plamkata
20th July 2008, 23:45
Hi,

I succeeded to fix it. It just needs to call area.setWidgetResizable(true) and it now works as expected

Thanks,
Plamen