Qt World Summit: Register Today!

QGLWidget appears black at runtime

  • Hello!

    I have been working on a program for about a week now and I have hit a bit of a wall. So on my ui form, I have a widget that has been promoted to having a base class of "GLWidget", as defined in my code. I needed a way to test out this widget, so I pulled the code out of the Qt5 3D cube example project and placed it in my class. All the required libraries are included, and everything compiles. But when the actual form appears, the GLWidget is solid black. At first I just assumed that the code for the widget simply wasn't executing. So, I put a breakpoint inside the paintGL() function, only for it to trigger at runtime. So clearly all the code for the widget is executing, it's just not appearing for whatever reason.

    What's even stranger is that this cube code is identical to the code in the Qt Creator project. The Creator project runs fine, I can see the cube onscreen. But in Visual Studio? The cube simply refuses to appear.
    I have searched all over the internet for solutions, but none of them worked.

    Hopefully someone out there can help me, as I am sure this is some n00b mistake on my end.

  • Moderators

    Well, start simple - remove all your drawing code and put there something like
    @glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    to see if it's a drawing code problem or something with the widget itself.

    Also, what type of Qt config are you using - precompiled package or self configured, which compilers, openGL desktop or ANGLE etc.

  • Thanks for the response!

    I just cleared out the drawing code and replaced it, but still a black screen.
    And as for the Qt configuration? Well I'm not entirely sure, but I created the project in Visual Studio as a "Qt Application" and left the options alone. I'm using the compilers included with Visual Studio (Also, the plugin handles some compiling of course.)
    I'm just using the standard libraries for openGL, but in this project I actually just reference the QtOpenGL files rather than gl.h and those headers.

    Sorry for the vague explanation, like I said, I'm rather new to Qt.

  • Just as a quick update:

    I removed the calls in my main.cpp program that display the UI. I now have code similar to this, effectively bypassing the widget entirely. However, I still have the same problem.
    #include <QApplication>
    #include <QLabel>

    #ifndef QT_NO_OPENGL
    #include "GLWidget.h"

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    #ifndef QT_NO_OPENGL
    GLWidget w;
    w.resize(640, 480);
    QLabel * notifyLabel = new QLabel("OpenGL Support required");
    return a.exec();

  • Ah I finally figured it out.
    It turns out that the version of OpenGL that came bundled was outdated, thus causing the black screen issue.

    Thanks for the suggestions!

  • So how did you solve this? Probably changing something in the properties of the visual studio project, but what?

  • Ok, so I had the similar issue. My problem was caused by a wrong resource location of the shader files. During building this doesn't cause an error and the screen indeed shows black.
    Only in debug mode, visual studio shows me that it can't find the shader files.
    After solving this, it all works fine.

    So my solution:

    1. Place the vertex and fragment shader file in the same folder as the glwidget.cpp and .h file.
    2. Add the files to the resource file (*.qrc file)
    3. Check in the resource manager what the url of the file is and
      make this the same in your code where it is loading the shader files.

Log in to reply