2

I can't quite figure out what the best way of displaying an image is in my particular case, so hopefully someone on here has a few tips.

I want to display an image that gets re-sized automatically to fit inside the space that is available. I currently do this by creating a class derived from QLabel that implements void resizeEvent(QResizeEvent*) where I do a QPixmap::scaled to re-size the image. The problem is that this only works when the widget is enlarged because the widget doesn't get a resizeEvent when I try to make the widget smaller. I guess that because I set the image to the same size as the widget, it isn't allowed to be sized smaller again? I guess I could try to create a smaller image therefor introducing a sort of "border" around the image which would perhaps allow re-size events to occur when making the area smaller. Any thoughts?

Lieuwe
  • 1,734
  • 2
  • 27
  • 41

2 Answers2

0

resizeEvent is sent whenever size is changed. It doesn't matter whether it is enlarged or not. But you can set Policy and Max/Min size to constraint widget in shrinking/enlarging. So if you have your widget not getting resizeEvent AND it doesn't shrink either, then look at your size policy and min width/height. If it shrinks but you doesn't have resizeEvent then you have some error in you logic, I believe.

Alternatively you can use paintEvent for image painting and use QWidget::rect() for your widget width/height.

ixSci
  • 13,100
  • 5
  • 45
  • 79
  • it doesn't shrink (and hence it isn't getting a resizeEvent) .. I'll have a look at this resize policy – Lieuwe Feb 19 '13 at 11:49
0

Try changing the size policy of the label to QSizePolicy::Preferred.

Have a look at size policies in general.

Niklas
  • 249
  • 3
  • 7