PDA

View Full Version : QLabel size, for image display



C167
9th January 2008, 15:29
Hi there,
the small imageViewer i write has two big problems: i can't find out the size of the label, so i cant resize it. And i don't know wo to find out what the new size is when the application gets resized.

A normal QLabel::size() doesn't report what i need. ATM, i have a 2000px*1500px image loaded in an QLabel, and set
imageLabel->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );to avoid that the label automaticaly adjusts its size so display the whole image. I have to places, where i use QLabels to display images, one is the overview page, where i have 5 QLabels in a QStackedLayout, and one in the imageViewer itself. Both are added to a Layout.
Any hints?
C167 :)

jacek
9th January 2008, 15:36
You can ask QLabel to scale the image, by setting QLabel::scaledContents property to true. Other solution is to add scrollbars using QScrollArea.

When it comes to sizes, size() returns valid data only after you show the widget for the first time.

C167
9th January 2008, 16:03
hm... but that resizes them by stretching... that might look ugly when viewing photos, where the proportions are important. I thought about QImage::scaled(), but for that i need the actual size and (thats important) get noticed if the window gets resized (this resizes a QVBoxLayout in one case, where the label uses the most place it gets per default).

I added a
qDebug() << label0->size(); immediatly after setting the pixmap. the result is always the same: QSize(100, 30).

I already searched for a signal that gets emited when when resizing, but found none... The resizing of the images itself should not be the problem, as mentioned above, using QImage.
Or is there a better way? painting many pixmaps into one QLabel in a short period of time is rather slow.

aamer4yu
11th January 2008, 14:34
Cant u get the size from size() as Jacek said ??
also QLabel is inherited from QWidget, so geometry() will also be available.

C167
11th January 2008, 14:44
I got a new hdd, so i can't continue developing. Would it be possible to get the size if i add a QWidget into the QVBoxLayout and add the label to it? or any other constellation?

jacek
12th January 2008, 19:03
Yes, but you have to show that widget or at least wait for it to be polished, before you will get a valid size.

C167
21st January 2008, 11:32
nope, i tried several things to get the size, using widgets, labels etc. every time i want to have the size i get a QSize(100,30) (or the equivalent using geometry()) so i have no chance to resize the images using QImage :(

ashukla
21st January 2008, 11:51
nope, i tried several things to get the size, using widgets, labels etc. every time i want to have the size i get a QSize(100,30) (or the equivalent using geometry()) so i have no chance to resize the images using QImage :(
I hope this code help you.

QLabel *lab=new QLabel(0);
QPixmap *p=new QPixmap("plane.jpg");
QPixmap p1(p->scaled ( 300,300, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ));
lab->setPixmap(p1);
lab->show();
lab->setFixedHeight(p1.height());
lab->setFixedWidth(p1.width());

ashukla
21st January 2008, 11:56
lab->setFixedHeight(p1.height());
lab->setFixedWidth(p1.width());
you can also use
lab->adjustSize(); in spite of above.

C167
21st January 2008, 18:35
thanks :)
now i understand where i was wrong. your code is much easier than my resize-function. Now the main problem to solve: how to get the maximum size the label can occupy?
I attached two screenshots. The first shows the splash, i changed the window-size to be a bit more than the fixed size of the label. Here, you can see the following structure:

QMainWindow->setCentralWidget(QStackedWidget)->QWidget->QStackedLayout->QLabel where the label at the end holds the image that you can see. this should always use the maximum size, like your code does. I thought i could get the size by using a QWidget instead of the label and attach the label to the widgets layout (i used QVBoxLayout).

The next screenshot shows the same problem. The area with a black border is a QLabel, that should also display images, but now with Qt::KeepAspectRatio. The Label should resize when window resizes, but by keeping the proportions of the file (image.size=label.size). Would be quite easy i thought some weeks ago...

ashukla
22nd January 2008, 10:04
how to get the maximum size the label can occupy
Use maximumSize() and setMaximumSize() for this.
And use this code for your problem;

#include <QtGui>
int main (int argc, char **argv)
{

QApplication app(argc,argv);
QWidget w;

QLabel *lab=new QLabel(&w);
QPixmap *p=new QPixmap("plane.jpg");
QPixmap p1(p->scaled ( lab->width(),lab->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation ));
lab->setPixmap(p1);
lab->show();
lab->adjustSize();
w.show();
w.resize(300,200);
return app.exec();

}

C167
22nd January 2008, 14:15
how to get the maximum size the label can occupy
Use maximumSize() and setMaximumSize() for this.i mean the maximum size inside the widget, the maximum is QSize(16777215, 16777215)...


And use this code for your problem;

#include <QtGui>
int main (int argc, char **argv)
{

QApplication app(argc,argv);
QWidget w;

QLabel *lab=new QLabel(&w);
QPixmap *p=new QPixmap("plane.jpg");
QPixmap p1(p->scaled ( lab->width(),lab->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation ));
lab->setPixmap(p1);
lab->show();
lab->adjustSize();
w.show();
w.resize(300,200);
return app.exec();
}
that generates a nixe little window with the picture at 100*30 in the top left corner...

ashukla
23rd January 2008, 07:31
You can set and use after the maximumSize of a widget using maximumSize() and setMaximumSize().

astodolski
25th October 2013, 16:09
i mean the maximum size inside the widget, the maximum is QSize(16777215, 16777215)...


that generates a nixe little window with the picture at 100*30 in the top left corner...

How is that a solution? If I read the comments correctly you are adjusting the label to be displayed as BIG as it can be. yet it displays a 100 x 30 image.
I am experiencing the same problem and don't seem to be able to display an image other than 100 x 30 size