Tried to refactor OpenGL demo slightly (and failed)

  • I took the OpenGL triangle demo and tried to modify it to make the constructor of the triangle class do much more, so there are fewer calls from main. In the process, I have a runtime error:

    //QOpenGLFunctions created with non-current context

    Here is the code. I think I preserved the order of what happened from the original code. What am I doing wrong?

    class TriangleWindow : public OpenGLWindow {
        const char* vertexShaderSource;
        const char* fragmentShaderSource;
        GLuint m_posAttr;
        GLuint m_colAttr;
        GLuint m_matrixUniform;
        QOpenGLShaderProgram *m_program;
        int m_frame;
        GLuint loadShader(GLenum type, const char *source) {
            GLuint shader = glCreateShader(type);
            glShaderSource(shader, 1, &source, 0);
            return shader;
        TriangleWindow(const char* vertexShaderSource, const char* fragmentShaderSource) :
            m_frame(0), vertexShaderSource(vertexShaderSource),  fragmentShaderSource(fragmentShaderSource){
            resize(640, 480);
        void initialize() Q_DECL_OVERRIDE {
            m_program = new QOpenGLShaderProgram(this);
            m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
            m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
            m_posAttr = m_program->attributeLocation("posAttr");
            m_colAttr = m_program->attributeLocation("colAttr");
            m_matrixUniform = m_program->uniformLocation("matrix");
            QSurfaceFormat format;
        void render() Q_DECL_OVERRIDE;
    //! [1]
    //! [2]
    int main(int argc, char **argv)
        QGuiApplication app(argc, argv);
        static const char *vertexShaderSource =
            "attribute highp vec4 posAttr;\n"
            "attribute lowp vec4 colAttr;\n"
            "varying lowp vec4 col;\n"
            "uniform highp mat4 matrix;\n"
            "void main() {\n"
            "   col = colAttr;\n"
            "   gl_Position = matrix * posAttr;\n"
        static const char *fragmentShaderSource =
            "varying lowp vec4 col;\n"
            "void main() {\n"
            "   gl_FragColor = col;\n"
        TriangleWindow window(vertexShaderSource, fragmentShaderSource);;
        return app.exec();

  • Lifetime Qt Champion


    Why not let initialize get called like the original code does it ?

    When your TriangleWindow gets constructed you don't have a context created yet.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.