Rendering an OpenGL ES 2.0 triangle using QGLWidget in Qt5.3

  • Hi all.
    Actually I am new to both OpenGL ES and Qt.So please don't mind if I go wrong anywhere.
    I am trying to render a simple triangle with interpolated vertices.When I compile it shows following errors.

    @error: 'initializeGLFunctions' was not declared in this scope
    error: 'glGenBuffers' was not declared in this scope
    error: 'program' was not declared in this scope
    positionID = program.attributeLocation ("s_vPosition");
    error: 'glVertexAttribPointer' was not declared in this scope
    glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0);
    error: 'glEnableVertexAttribArray' was not declared in this scope
    error: 'glBindBuffer' was not declared in this scope
    error: 'glBufferData' was not declared in this scope@

    In one of the Forums I found that its because of exclusion of GLEW(not sure why it is used).So I went ahead and added #include<GL/glew.h>. But it made the thing worst,I got 121 errors.

    This is my code written in Qt Creator. The code below is Geometry.cpp which inherits QGLWidget as Base Class.

    @#include "geometry.h"
    #include <QVector3D>
    #include <QtOpenGL/QGLFunctions>
    #include <QGLShaderProgram>
    #include <QtOpenGL/qgl.h>

    Geometry::Geometry(QWidget *parent) :

    void Geometry::initializeGL()
    glGenBuffers(1, &vbo);
    positionID = program.attributeLocation ("s_vPosition");// return the program id which is stored in positionID
    colorID= program.attributeLocation("s_vColor");
    // glewInit();

    void Geometry::resizeGL(int w, int h)
    glViewport(0, 0, w, h);

    void Geometry::paintGL()

    glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glVertexAttribPointer(colorID, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(3*3*sizeof(GLfloat)));
    glDrawArrays(GL_TRIANGLES, 0, 3);


    void Geometry::initShaders()
    // Compile vertex shader
    if (!program.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.glsl"))

    // Compile fragment shader
    if (!program.addShaderFromSourceFile&#40;QGLShader::Fragment, ":/fragmen--tShader.glsl"&#41;)
    // Link shader pipeline
    if (!
    // Bind shader pipeline for use
    if (!program.bind())


    void Geometry::initTriangleGeometry()
    GLfloat vertices[]={-0.5f, -0.5f, 0.0f,
    0.5f, -0.5f, 0.0f,
    0.0f, 0.5f, 0.0f};

    GLfloat colors[]={1.0f, 0.0f, 0.0f, 1.0f,
                       0.0f, 1.0f, 0.0f, 1.0f,
                       0.0f, 0.0f, 1.0f, 1.0f};
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, 7*3*sizeof(GLfloat), NULL, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, 3*3*sizeof(GLfloat), vertices);
    glBufferSubData(GL_ARRAY_BUFFER, 3*3*sizeof(GLfloat),3*4*sizeof(GLfloat), colors);



    @#include "mainwindow.h"
    #include <QApplication>
    #include <geometry.h>

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    Geometry widget;

    return a.exec&#40;&#41;;


    Vertex Shader:

    @attribute vec3 s_vPosition;
    attribute vec4 s_vColor;
    varying vec4 color;

    void main () {
    color = s_vColor;
    gl_Position = vec4(s_vPosition, 1.0);

    Fragmnet Shader:
    @varying vec4 color;

    void main () {
    gl_FragColor = color;

    Please Help.

  • codefy,
    the following link is to a youtube video of someone doing exactly what you want

    however, I encourage you to look into QWindow. As I understand, the Qt project is trying to move away from the QGLWidget. They want to use QWindow only. Use createWindowContainer() on your MainWindow class to create a drawable OpenGL surface, the QWindow.

    Hope this works.

  • Lifetime Qt Champion


    No they are not, QGLWidget will have a replacement called QOpenGLWidget.

  • Thank you both of you for your reply.

    Actually I have referred this video before it is to render a triangle using fixed rendering pipeline but I want to do it using programmable pipeline.What about the Error any idea?

    What does that mean can you please explain it precisely?

  • Moderators

    A new, better class will be introduced in Qt 5.4 (alpha version will be available this week, or early next week): QOpenGLWidget. You can read some discussion about it here: "link":

  • You are most likely not deriving from QGLFunctions, hence the absence of initializeGLFunctions and the GL2 stuff. The Geometry class should derive both from QGLWidget and QGLFunctions:
    class Geometry : public QGLWidget, protected QGLFunctions { ... };

  • According to Qt's roadmap, Qt 5.4 will have all QGL* classes and functions deprecated. Try using QOpenGL* classes instead.

  • Thanks all for commenting here.

    Why we need multiple inheritance.?How will it solve my problems?

    That really was an useful information.

    I tried it but no luck. Seems like Qt5.3.1 does not supports it.Not sure.

Log in to reply

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