Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force QGLWidget to update screen?

Tags:

c++

qt

opengl

I’m drawing a simple scene with Open GL. I’ve subclassed QGLWidget and overriden paintGL(). Nothing fancy there:

void CGLWidget::paintGL()
 {
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt (120.0, 160.0, -300.0, 0.0 + 120.0, 0.0 + 160.0, 2.0 - 300.0, 0.0, 1.0, 0.0);
    glScalef(1.0f/300.0f, 1.0f/300.0f, 1.0f/300.0f);
    glClear(GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(80.0, width()/(double)height(), 5.0, 100000.0);

    glMatrixMode(GL_MODELVIEW);
    glBegin(GL_POINTS);
    glColor3f(1.0f,0.6f,0.0f);
    glVertex3d(x, y, z);

    // ...drawing some more points...

    glEnd();

 }

I have a timer in main window which triggers updateGL() of GL widget. I’ve verified that it results in paintGL() being called. However, the actual picture on the screen is only updated very rarely. Even if I resize the window, scene is not updated. Why is that and how can I force it to update?

like image 271
Violet Giraffe Avatar asked Feb 21 '13 12:02

Violet Giraffe


2 Answers

Don't call updateGL() from your timer, call update() instead to ensure that the view gets a paint event.

like image 188
Pete Avatar answered Sep 20 '22 11:09

Pete


inside your CGLWidget constructor

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(10);

timer -> start(VALUE);

play with `VALUE 10 is just an example.

Adorn

like image 21
Adorn Avatar answered Sep 21 '22 11:09

Adorn