PDA

View Full Version : calling setFont() twice causes infinite repaint



Cruz
17th January 2012, 22:17
Is it not possible to change the font twice during a paintEvent()? The following minimal example causes an infinite loop of paintEvent()s, if the font is changed twice. If I comment any of the setFont() lines out, it works fine.




class FontRepaint : public QWidget
{
Q_OBJECT

public:
FontRepaint(QWidget *parent = 0);
~FontRepaint(){};

protected:
void paintEvent(QPaintEvent*);

};

FontRepaint::FontRepaint(QWidget *parent)
: QWidget(parent)
{

}

void FontRepaint::paintEvent(QPaintEvent*)
{
qDebug() << "repaint";
setFont(QFont("Helvetica", 18));
setFont(QFont("Helvetica", 14));
}

wysota
17th January 2012, 22:22
Since you change the font in the paint event, the widget has to be repainted. If there is one call to setFont, then the font is changed at most once (since next time paint event is called the font is already set to what you are trying to assign) so paint event is only triggered one time too many. Paint event is for painting, not setting widget attributes (and this thread is a fine example to why I claim paintEvent should be const).