Results 1 to 6 of 6

Thread: Why paintGL() in QGLWidget is called twice

  1. #1
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question Why paintGL() in QGLWidget is called twice

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QtOpenGL/QGLWidget>
    3. #include <QtCore/QDebug>
    4.  
    5. class GLWidget : public QGLWidget
    6. {
    7. public:
    8. GLWidget(QWidget * parent = 0) : QGLWidget(parent) {}
    9.  
    10. protected:
    11. void paintGL() // why paintGL() is called twice
    12. {
    13. static int i = 0;
    14. qDebug() << ++i;
    15. }
    16. };
    17.  
    18. int main(int argc, char **argv)
    19. {
    20. QApplication app(argc, argv);
    21. GLWidget widget;
    22. widget.show();
    23. return app.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

    output:
    1
    2

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Why paintGL() in QGLWidget is called twice

    The widget is created with a default location and size. You show it. The widget is painted. The window manager adds window decorations and shifts the client area: the widget is repainted. The window manager decides to move it, the widget is repainted. The exact sequence of events depends on your environment a bit. Feel free to drop a debug message into the showEvent(), moveEvent(), resizeEvent() etc.

    Why do you care?

  3. #3
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why paintGL() in QGLWidget is called twice

    But why paintEvent() in QWidget is called just once?
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QtGui/QWidget>
    3. #include <QtCore/QDebug>
    4.  
    5. class Widget : public QWidget
    6. {
    7. public:
    8. Widget(QWidget * parent = 0) : QWidget(parent) {}
    9.  
    10. protected:
    11. void paintEvent(QPaintEvent */*event*/)
    12. {
    13. static int i = 0;
    14. qDebug() << ++i;
    15. }
    16. };
    17.  
    18. int main(int argc, char **argv)
    19. {
    20. QApplication app(argc, argv);
    21. Widget widget;
    22. widget.show();
    23. return app.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

    Output:
    1
    Last edited by wysota; 29th February 2012 at 10:09.

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

    Default Re: Why paintGL() in QGLWidget is called twice

    Why do you care?
    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.


  5. #5
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why paintGL() in QGLWidget is called twice

    Quote Originally Posted by wysota View Post
    Why do you care?
    I think it should be once. Program will take more resources if the function is called twice.

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

    Default Re: Why paintGL() in QGLWidget is called twice

    I can tell you it is going to be called many many times more once you start doing anything with your program.
    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.


Similar Threads

  1. QGLWidget's PaintGl Method
    By beginQT in forum Newbie
    Replies: 5
    Last Post: 25th September 2011, 13:48
  2. Replies: 7
    Last Post: 1st July 2011, 02:21
  3. Replies: 1
    Last Post: 27th June 2011, 22:37
  4. Help on OpenGL in PaintGL
    By mickey in forum Newbie
    Replies: 1
    Last Post: 15th April 2006, 23:17
  5. QGLWidget, toolbox and paintGL()
    By mickey in forum Qt Programming
    Replies: 3
    Last Post: 21st March 2006, 02:05

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.