image shown in Qlabel -> size increases
Hi,
I have a main window, I've set a gridlayout on it. I placed some QLabels into the grids where I show images using QLabel::setPixmap(). Thnew images should be shown at some user interactions: pushing buttons, clicking on radiobuttons etc. Now I cannot imagine why but the size of the labels get bigger and bigger on each click. The images are loaded in this way:
ui->label_img->setPixmap(myimg.scaled(ui->label_img->width(),ui->label_img->height(),Qt::KeepAspectRatio));
This is what I would like to solve without setting any size fixed. (window should stay "flexible")
http://youtu.be/YgzcuOXF5RI
Szilvi
Re: image shown in Qlabel -> size increases
The pixmap size follows the label. What in your code is changing the size of the label?
Re: image shown in Qlabel -> size increases
I've changed now the QLabels' sizePolicy to "Ignored" in the in-built Qt Designer. Now it works fine. But why does it increase otherwise?
Re: image shown in Qlabel -> size increases
Do you have a style sheet attached to the label? Does it add a margin or padding? If so, each time you scale the image you add the margin/padding to it size.
This, for example, does not change the size of the label every two seconds:
Code:
#include <QtGui>
#include <QDebug>
Q_OBJECT
public:
image.load("test.png");
label1->setPixmap(image);
qDebug() << image.size() << label1->size();
label2
= new QLabel("Label 2",
this);
layout->addWidget(label1);
layout->addWidget(label2);
central->setLayout(layout);
setCentralWidget(central);
connect(&t, SIGNAL(timeout()), SLOT(tick()));
t.start(2000);
}
public slots:
void tick() {
QPixmap temp
= image.
scaled(label1
->width
(), label1
->height
(), Qt
::KeepAspectRatio);
qDebug() << temp.size() << label1->size();
label1->setPixmap(temp);
}
private:
};
int main(int argc, char *argv[])
{
MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"
unless you add:
Code:
qApp->setStyleSheet("QLabel { margin: 5px; }");
in main().