# How to enable depth test with QOpenGLWidget? It renders black screen

Currently I am writing a small application with Qt and OpenGl and I choosed QOpenGLWidget for rendering graphics.

That's how I declared my widget:

class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions{
// Methods, slots, e.t.c
}


And that's the constructor:

GLWidget::GLWidget(QWidget *parent) : QOpenGLWidget(parent)
{
/* Tried this, it didn't help.
QSurfaceFormat format;
format.setDepthBufferSize(24);
this->setFormat(format);
*/
}


In my init function I set GL_CULL_FACE and GL_DEPTH_TEST:

void GLWidget::initializeGL()
{
/* Some initialization stuff regarding the scene */

glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

}


And I really don't know why the widget renders the black screen. Here are some pictures. First one: with disabled GL_CULL_FACE and disabled GL_DEPTH_TEST.

Second one: with enabled GL_CULL_FACE and disabled GL_DEPTH_TEST. Maybe it's not a good picture, but I can assure you, you can see some surfaces through the other.

Third one: with enabled GL_CULL_FACE and enabled GL_DEPTH_TEST. Actually, it doesn't matter if GL_CULL_FACE is enabled. Anyway it renders the black screen.

And here's the image without any shading just to show you that the model is fine.

I tried to set the format manually, but it didn't help. Still the black screen:

QSurfaceFormat format;
format.setDepthBufferSize(24);
this->setFormat(format);


Oh and yes, I set glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); at the beginning of my paintGL() function.

• By the way, if I try to set the version with format.setVersion(3, 2);, it also renders the black screen. I am not sure, but I think it's somehow related to the problem. – user3051029 May 28 '16 at 22:40