0
votes

I have a QLabel with a sizepolicy of QSizePolicy::Expanding in both dimensions in which I want to show an image with QLabel::setPixmap. The mainwindow should not be resized, that's why I scale the pixmap before setting to the size of the QLabel.

The image is set correctly, but on top and on bottom there is a padding I don't want, on top 12px, bottom 13px. The setPixmap-command resizes the QLabel and the mainwindow. I can't figure out why. The stylesheet of the QLabel is empty. I can color the QLabel-background to see that the extra space is a padding of the QLabel (or margin of the pixmap). When I try to resize the mainwindow, I can't get it so small that this extra border disappears, I can enlarge it and the padding increases on all sides (like expected), but I can't get it smaller to get rid of this extra margin.

Does anybody know what could cause this?

EDIT:

added some code. Before these lines ui->label_title is empty and label_title is in the same vertical layout just on top of label_main. I just thought, that when some text is set to label_title, the layout is not yet updated, so the next line returns the old size and then the image is too big, because the title took some space and the mainwindow has to be resized. Is there a command to update the layout, so all widgets in it get their new size? layout->update(); did'nt work.

ui->label_title->setText(imgNames.at(crntItemNr));
QSize size = ui->label_main->size();
ui->label_main->setPixmap(images.at(crntItemNr).scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
2

2 Answers

1
votes

Problem was the wordwrap of label_title as explained at the doc. The behaviour of the label_main was strange, that made it hard to find the cause in label_title.

0
votes

It is probably because you are using the wrong measurements for your window size. You are thinking in terms of the box model, and accessing the geometry and it is measuring differently:

http://qt-project.org/doc/qt-5.0/stylesheet-customizing.html

http://qt-project.org/doc/qt-4.8/application-windows.html#window-geometry

It is difficult to see exactly what is wrong without a code sample.

I hope that helps.