PDA

View Full Version : image shown in Qlabel -> size increases



szisziszilvi
15th November 2011, 21:33
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

ChrisW67
15th November 2011, 22:58
The pixmap size follows the label. What in your code is changing the size of the label?

szisziszilvi
16th November 2011, 09:54
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?

ChrisW67
16th November 2011, 10:17
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:

#include <QtGui>
#include <QDebug>

class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *p = 0): QMainWindow(p) {

QWidget *central = new QWidget(this);
label1 = new QLabel(this);
image.load("test.png");
label1->setPixmap(image);
qDebug() << image.size() << label1->size();
label2 = new QLabel("Label 2", this);

QVBoxLayout *layout = new QVBoxLayout(central);
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:
QPixmap image;
QLabel *label1 ;
QLabel *label2 ;
QTimer t;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"

unless you add:


qApp->setStyleSheet("QLabel { margin: 5px; }");
in main().