PDA

View Full Version : Dynamic Layout: Resizing QLabel, Scaling the pixmap, gridlayout



hus787
8th October 2011, 12:35
Well, it's been like three days since im trying to figure out how to deal with this but can't get the desired result. So please i need some expert guidance.

What im tryna build is a dynamic layout which, for now, displays variable number of pictures of different dimensions(resolutions) on a non-scrollable/static pane(widget) with using qlabels - used to display the picture - , with each getting an equal share on the pane, while ENSURING THE ASPECT RATIO of the picture remain intact.

Right now i've been trying to achieve this with just two picture, using gridlayout and if succeeded could be implemented for multiple pictures. The main problem is keeping the aspect ratio intact while ensuring that the maximum amount of space is occupied on the pane.

One way i achieved the desired result was by using:


grid->addWidget(cam,0,0);
grid->addWidget(cam_1,0,1);

which wouldn't be practical for variable no. of pictures, to achieve an nicely distributed gridlayout. cuz ull have to design an algorithm which would determine the numbers of row and column in which each would go.

wat is practical, to me, is, grid->addwidget(QWidget*) but with that, things go all wacky, or if it works by holding on to the aspect ratio the pixmap is all downsized and sometime microscopic.

following is 1 of the variety of concepts i applied



QString file="D:/Pics/Misc/1115.jpg";
QImage pic(file);

Widget* w=new Widget();
QGridLayout* gridmain=new QGridLayout(w);
QWidget* widget=new QWidget(w);
w->widget->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixe d);
QLabel* cam=new QLabel(w->widget);
QLabel* cam_1=new QLabel(w->widget);
QGridLayout* grid=new QGridLayout(w->widget);
cam_1->setSizePolicy(QSizePolicy::Expanding,QSizePolicy:: Expanding);
cam->setSizePolicy(QSizePolicy::Expanding,QSizePolicy:: Expanding);
// cam->setScaledContents(true); commented cuz it doesn't take the aspect ratio into consideration
// cam_1->setScaledContents(true);

grid->addWidget(cam_1);
cam_1->setPixmap(QPixmap::fromImage(pic).scaled(grid->itemAt(0)->sizeHint(),Qt::KeepAspectRatio));
cam->resize(cam_1->size());
cam->setPixmap(QPixmap::QPixmap(QFileDialog::getOpenFil eName(0,"Open File",QString::null,QString::null)).scaled(cam->size(),Qt::KeepAspectRatio));
w->widget->setLayout(grid);

If somehow i get the size of the each grid cell(or even one, cuz i want all the cells of equal size) after setting the layout to the parent widget i could scale pixmap to that size.

Please help.