PDA

View Full Version : Change the size of pixmap in a label



metRo_
26th May 2010, 16:43
Hi,
I creat a label and add a image to pixmap of label:
#include "mainwindow.h"
#include <QtGui>

MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
QHBoxLayout *lay=new QHBoxLayout;
setLayout(lay);
QImage image("comando.jpg");
QLabel *imageLabel = new QLabel(this);
lay->addWidget(imageLabel);
imageLabel->setPixmap(QPixmap::fromImage(image));
}

MainWindow::~MainWindow()
{

}

and now i want to resize the image. I want set a size to the label like 200*200 and resize the image to it.

tbscope
26th May 2010, 16:56
http://doc.qt.nokia.com/4.6/qimage.html#scaled-2

metRo_
26th May 2010, 17:22
#include "mainwindow.h"
#include <QtGui>

MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
QHBoxLayout *lay=new QHBoxLayout;
setLayout(lay);
QImage image("comando.jpg");
image.scaled(200,200,Qt::IgnoreAspectRatio,Qt::Fas tTransformation);
QLabel *imageLabel = new QLabel(this);
lay->addWidget(imageLabel);
imageLabel->setPixmap(QPixmap::fromImage(image));
qDebug() << imageLabel->pixmap()->size();
}

MainWindow::~MainWindow()
{

}

Thanks but it's still the same, the image load into the label with original size :S

tbscope
26th May 2010, 17:28
The Qt documentation is one of the best. I wonder why people don't read it.


Returns a copy of the image scaled to a rectangle with the given width and height

metRo_
26th May 2010, 17:32
you're right, thanks very much :D

metRo_
26th May 2010, 17:42
QPixmap QPixmap::scaled ( int width, int height, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation ) const
How can i use it?

even is i use like this: QSize tam(200,200);
imageLabel->pixmap()->scaled(tam,Qt::IgnoreAspectRatio,Qt::FastTransform ation); it doesn't change the size of the picture, why i have to use the QImage scaled instead of it?

tbscope
26th May 2010, 17:50
What happens if you use:


imageLabel->setPixmap(QPixmap::fromImage(image.scaled(200,200) ));

metRo_
26th May 2010, 18:08
it's work but this or that "image = image.scaled(200,200,Qt::IgnoreAspectRatio,Qt::Fas tTransformation);" is the same. i tallking about the scaled function for pixmap.

metRo_
26th May 2010, 18:09
it's work, but this is the same of "image = image.scaled(200,200,Qt::IgnoreAspectRatio,Qt::Fas tTransformation);".
Why i can't use the scaled function of pixmap.

tbscope
26th May 2010, 18:12
Who said you can't?

metRo_
26th May 2010, 20:36
i tried and the image doesn't resize! :S

metRo_
27th May 2010, 00:36
#include "mainwindow.h"
#include <QtGui>

MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
QHBoxLayout *lay=new QHBoxLayout;
setLayout(lay);
QImage image("comando.jpg");
QLabel *imageLabel = new QLabel(this);
lay->addWidget(imageLabel);
imageLabel->setPixmap(QPixmap::fromImage(image.scaled(400,400, Qt::KeepAspectRatio,Qt::FastTransformation)));
qDebug() << imageLabel->pixmap()->size();
}

MainWindow::~MainWindow()
{

}

Now all the time i want change the imagelabel i will need to set again the pixmap :s I resize the image all the time that the user will change the mainwindow size.

dflo
8th April 2011, 05:44
metRo_: Yes, that is true, whether you resize the image and extract its pixmap each time, or save the extracted pixmap and resize the pixmap each time. If the label can change size and you want the pixmap to change size with it, somebody has to resize the pixmap. In Qt, that somebody is you.

One option is to subclass QLabel and override the resizeEvent() method. Within that, resize the pixmap (from the original! don't keep resizing the resized image!), and call setPixmap(). So you probably want a setOriginalPixmap() method also, in case you want to change the image that's on the the label.



void setOriginalPixmap(QPixmap *pixmap)
{
if (original_pixmap)
delete original_pixmap;
original_pixmap = pixmap;
setScaledPixmap();
}

void resizeEvent (QResizeEvent *event)
{
setScaledPixmap();
}

void setScaledPixmap (void)
{
scaled_pixmap = original_pixmap->scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
setPixmap(scaled_pixmap);
}

JohannesMunk
8th April 2011, 06:21
@dflo: Welcome!

But this thread is quite old to be picked up, don't you think?

Joh

dflo
8th April 2011, 06:30
Yeah probably. I was looking for something else, and meanwhile I found some questions I could answer. Maybe metRo_ put his project on hold and will come back to it? :)

Thanks for the welcome!

JohannesMunk
8th April 2011, 06:35
Yeah :-> Just don't be disappointed if nothing happens - that's all I wanted to say.

Happy coding,

Johannes

sonulohani
1st June 2012, 10:21
First set the pixmap to the label.
Then, set the size of the label by:-


QLabel label=new QLabel;
label.setPixmap(pix);
label.setFixedSize(w,h);