View Full Version : Making QT believe a widget is bigger

23rd September 2006, 09:58

I have put a nuch of QLabels in a QGridLayout and given each a small QPixmap to get a collection of thumbnails.

Now I want to draw some decoration outside each pixmap/label. I can't simply reimplement paintEvent() of the QLabels and draw the extra stuff there because the drawing is clipped to a region with the exact size of the pixmap.

Well, QLabels are QFrames which can draw stuff outside, so my first approach was to put e.g.

setFrameStyle(QFrame::Panel | QFrame::Plain);

in the constructor of Thumb (which inherits QLabel) to trick QT into believing it is about to draw a one-pixel frame around the QLabel.
paintEvent() now gets a bigger region, and I can draw my own decoration there.

This works fine, but the actual extra width/height is dependent on the current style and I'd like to avoid that dependence.

What is the proper way of telling QT that a widget need to be a little bigger? For testing purposes, I tried:

QSize Thumb::sizeHint() const
return QLabel::sizeHint() + QSize(20,20);

From debugging I can see that my reimplemented sizeHint is indeed called, and returns the expected value.
The rect() of the paintEvents are now slightly bigger (by a pixel or two, nowhere near the 20 I asked for).
It seems that the extra paintEvent-rect size only ate into the QGridLayout's spacing().

How do I get QGridLayout to understand that each Thumb is bigger, if reimplementing sizeHint doesn't help?

/Joakim Rosqvist

23rd September 2006, 10:16
QWidget::setContentsMargins() (http://doc.trolltech.com/4.1/qwidget#setContentsMargins)

23rd September 2006, 13:17
Yup, that worked!

There is a QLabel::setMargin() that also seems to do the trick. That margin is apparently added to the contentsMargins. Redundant functionality?