You are not logged in.

1

Monday, April 2nd 2012, 11:52pm

calling glClear inside initializeGL instead of paintGL

Hello, I'm new to QT, and I hope this question doesn't come off as trivial and silly.

I've been trying to get glClear inside initializeGl to work, but to my surprise, it doesn't work. Here's a code fragment:

Source code

1
2
3
4
5
void GLWidget::initializeGL()
{
  glClearColor(0,0,0,1);
  glClear(GL_COLOR_BUFFER_BIT); // Doesn't work because the widget isn't cleared to black
}

Why can't I clear in initializeGl?

2

Thursday, April 5th 2012, 2:17pm

Hello, I'm new to QT, and I hope this question doesn't come off as trivial and silly.

I've been trying to get glClear inside initializeGl to work, but to my surprise, it doesn't work. Here's a code fragment:

Source code

1
2
3
4
5
void GLWidget::initializeGL()
{
  glClearColor(0,0,0,1);
  glClear(GL_COLOR_BUFFER_BIT); // Doesn't work because the widget isn't cleared to black
}

Why can't I clear in initializeGl?
What exactly doesn't work, could you post the error log or describe?
I may be ignorant, but why are you overwriting the method GLWidget::initializeGL() ? You have to make a Subclass of GLWidget...

It works for me like that:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include 


class widget : public QGLWidget
{
    Q_OBJECT
public:
    explicit widget(QWidget *parent = 0);
protected:
    void initializeGL();
};



widget::widget(QWidget *parent) :
    QGLWidget(parent)
{}


void widget::initializeGL()
{
    glClearColor(0.0,0.0,0.0,1.0);
    glClear(GL_COLOR_BUFFER_BIT);
}


Important is, when using OpenGL in Qt, that you change the Project file and add opengl to it:

Projectname.pro

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
QT       += core gui opengl



TARGET = OpenGL
TEMPLATE = app




SOURCES += main.cpp \
    widget.cpp


HEADERS  += \
    widget.h

Similar threads

Used tags

OpenGL