Results 1 to 2 of 2

Thread: calling setFont() twice causes infinite repaint

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default calling setFont() twice causes infinite repaint

    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.

    Qt Code:
    1. class FontRepaint : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. FontRepaint(QWidget *parent = 0);
    7. ~FontRepaint(){};
    8.  
    9. protected:
    10. void paintEvent(QPaintEvent*);
    11.  
    12. };
    13.  
    14. FontRepaint::FontRepaint(QWidget *parent)
    15. : QWidget(parent)
    16. {
    17.  
    18. }
    19.  
    20. void FontRepaint::paintEvent(QPaintEvent*)
    21. {
    22. qDebug() << "repaint";
    23. setFont(QFont("Helvetica", 18));
    24. setFont(QFont("Helvetica", 14));
    25. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: calling setFont() twice causes infinite repaint

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    Cruz (17th January 2012)

Similar Threads

  1. Replies: 4
    Last Post: 17th October 2010, 22:30
  2. QPushButton not calling repaint when needed?
    By Enygma in forum Qt Programming
    Replies: 5
    Last Post: 13th May 2010, 17:03
  3. Problem with setFont for a PushButton
    By arunvv in forum Newbie
    Replies: 12
    Last Post: 7th April 2008, 23:35
  4. QPainter, scale(), and setFont()
    By c_07 in forum Qt Programming
    Replies: 11
    Last Post: 2nd January 2008, 19:46
  5. Replies: 5
    Last Post: 20th November 2007, 12:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.