PDA

View Full Version : Dynamic Scrollarea Content



rouge
13th May 2011, 13:58
hi
I would like to have a scrollarea contains a dynamic count of images...

I tried to solve it like you see below. But the problem is that the scrollbar never appear...

My approach is:
I design with the QT desinger a ScrollArea (scrollArea) with default ScrollBarAsNeeded. Then I at also in the QT Designer a widget to the ScrollArea (named scrollContent).

Then in the Programm i tried:


QVBoxLayout* ScrollBoxLayout = new QVBoxLayout();
ScrollBoxLayout->setSpacing(2);
ScrollBoxLayout->setMargin(2);
ScrollBoxLayout->setAlignment(Qt::AlignTop);
ScrollBoxLayout->setSizeConstraint(QLayout::SetMinimumSize);
ui.scrollArea->setLayout(ScrollBoxLayout);


and add the images like:



for (it=sig.begin(); it!=sig.end(); it++){
//ignore this part
image = QImage((uchar *) it->Image()->imageData, it->Image()->width, it->Image()->height,QImage::Format_RGB888);
image = image.rgbSwapped();

//add qlabel to widget in scrollarea
QLabel *img = new QLabel();
ui.scrollArea->layout()->addWidget(img);
img->setPixmap(QPixmap::fromImage(image));
img->repaint();
}


so the images appears like I wanted row by row... but no scrollbar appears if there are many QLabel objects added...

thx

wysota
13th May 2011, 14:30
Set the widgetResizable property of the scroll area to true.

rouge
13th May 2011, 14:40
hmm that is already on true...

i also set the maximum height in scrollarea but the area grows with the content...

wysota
13th May 2011, 14:50
This works for me:

#include <QtGui>

int main(int argc, char **argv){
QApplication app(argc, argv);
QScrollArea scroll;
scroll.setVerticalScrollBarPolicy(Qt::ScrollBarAsN eeded); // it's default anyway
QWidget *viewport = new QWidget;
scroll.setWidget(viewport);
scroll.setWidgetResizable(true);
QVBoxLayout *l = new QVBoxLayout(viewport);
for(int i=0;i<50;++i) {
QPushButton *pb = new QPushButton(QString::number(i+1));
l->addWidget(pb);
}
scroll.show();
return app.exec();
}

rouge
13th May 2011, 17:04
yes this works...

but in my case I have a mainwindow and on that there is the scrollarea... and I try to add at runtime...
and I think thats my problem...

or is there something other wrong??'

Added after 26 minutes:

wow now it works!

the problem was that the scrollarea must have a layout too!!!

thx

wysota
13th May 2011, 17:42
the problem was that the scrollarea must have a layout too!!!
The problem is the code you provided states the scroll area has a layout. You probably mean the main window needs a layout.

rouge
14th May 2011, 09:37
The problem is the code you provided states the scroll area has a layout. You probably mean the main window needs a layout.

yes sure I mean the main window