PDA

View Full Version : Load Images in a dir into a QScrollArea



nmuntz
20th January 2009, 01:20
Hi,
I'm trying to load all the images in the "images/" directory into a QScrollArea but I'm not sure how to add images into a QScrollArea. Or perhaps should I use QGraphicsView?

I have a QStringList with all the images.

I probably need to create a QLabel for every image in the dir and setPixmap for all of them.
I'm just not sure on how to create the loop that iterates through QStringList and how to embed every QLabel into the QScrollArea (I only see setWidget but would that only embed one widget into it? )

Thanks a lot in advance for any help you can provide.

wysota
20th January 2009, 01:32
QWidget *viewport = new QWidget;
QVBoxLayout *l = new QVBoxLayout(viewport);
foreach(const QString &path, stringlist){
QPixmap px(path);
QLabel *label = new QLabel;
label->setPixmap(px);
l->addWidget(label);
}
scrollArea->setWidget(viewport);

This won't look very good but it's a place to start. Using QGraphicsView or QListView might give you better results though...

nmuntz
20th January 2009, 01:39
Thank you very much!
That's exactly what I was looking for !
Thanks again!

nmuntz
20th January 2009, 21:03
Hi,
I'm just curious now:

How could I handle each QLabel generated afterwards?
I have a central QLabel which should change the pixmap to the image that I click by using the code in this example.

Perhaps in the loop connect it to a signal that triggers a mousepressevent ?

Thanks in advance for pointing me in the right direction.