Results 1 to 4 of 4

Thread: qglwidget not always drawing

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2008
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default qglwidget not always drawing

    Hi, here's the code I'm working from:
    Qt Code:
    1. class GLTest : public QGLWidget {
    2. Q_OBJECT
    3. public:
    4. GLTest (QWidget * = NULL);
    5. protected:
    6. void initializeGL ();
    7. void resizeGL (int, int);
    8. void paintGL ();
    9. };
    10.  
    11. GLTest :: GLTest (QWidget * parent) : QGLWidget (parent) {
    12. QGLFormat f = format();
    13. f.setDoubleBuffer (true);
    14. setFormat (f);
    15. }
    16.  
    17. void GLTest :: initializeGL () {
    18. glClearColor (0.0, 0.0, 0.0, 0.0);
    19. }
    20.  
    21. void GLTest :: resizeGL (int w, int h) {
    22. glViewport (0, 0, (GLint)w, (GLint)h);
    23. }
    24.  
    25. void GLTest :: paintGL () {
    26. glClear (GL_COLOR_BUFFER_BIT);
    27.  
    28. glColor3f (1,0,0);
    29. glBegin (GL_QUADS);
    30. glVertex2f (0, 0);
    31. glVertex2f (150, 0);
    32. glVertex2f (150, 50);
    33. glVertex2f (0, 50);
    34. glEnd ();
    35.  
    36. swapBuffers ();
    37. };
    To copy to clipboard, switch view to plain text mode 

    The red-on-black is drawn as expected, but only when the window is being resized. The rest of the time the display shows lingering artefacts of e.g. moved window borders.

    Why isn't the image persistent?

    Thanks

  2. #2
    Join Date
    Jun 2009
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qglwidget not always drawing

    I think you have to call paintGL() by yourself (i.e. by a QTimer).

  3. #3
    Join Date
    Nov 2008
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qglwidget not always drawing

    With this code, the problem is more evident.

    Qt Code:
    1. int main (...) {
    2. // ...
    3. GLTest * gl_test = new GLTest ();
    4. gl_test -> resize (300,300);
    5. gl_test -> show ();
    6. QTimer :: singleShot (1000, gl_test, SLOT(update()) );
    7. }
    To copy to clipboard, switch view to plain text mode 

    After a second, the window is painted correctly. However as soon as anything disturbs it, another window passing in front for example, the image is destroyed.

    The problem with using a timer to trigger a refresh is that in between refreshes the image can be destroyed. If the update is happening at 0.2 frames per second the window SHOULD be able to repaint itself without updating in-between frames.

    Forced updates are a hack, not a solution, and this isn't OpenGL-specific -- ordinary windows do not have to perform a full-depth repaint every time the mouse passes over them!

    What have I got wrong?

  4. #4
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: qglwidget not always drawing

    Quote Originally Posted by h0nki View Post
    I think you have to call paintGL() by yourself (i.e. by a QTimer).
    updateGL(); and only if your objects are animated. In your static example you do not need any timer.

    Qt should redraw (ie, call updateGL automatically) every time you move a window over your program.

    I don't see any mistake on your code. Maybe you do not set up a view properly.
    Try clearing also Z-Buffer
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    And double buffer is activated by default, so remove that code, is useless.

    Another suggestion, glViewport modifies your proyection matrix, try to write this:
    glViewport (0, 0, (GLint)w, (GLint)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
    glOrtho or perspective

    And do not forget to load default identy matrix every time you draw something:
    glClear (GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glColor3f (1,0,0);
    ...

Similar Threads

  1. Using QWidgets on QGLWidget for 3D game interface
    By Statix in forum Qt Programming
    Replies: 3
    Last Post: 15th February 2010, 14:05
  2. Replies: 1
    Last Post: 23rd April 2009, 09:05
  3. Replies: 1
    Last Post: 21st November 2008, 07:00
  4. Problems with QString
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2008, 08:18
  5. Replies: 4
    Last Post: 7th May 2008, 00:01

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.