Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with OpenGL when running QT Creator

I am trying to run basic examples of OpenGL using QT Creator to give color to a window. However, I am getting error in the compilation when calling the OpenGL instruction: glClearColor(1.0,1.0,0.0,1.0); The *.pro file is the next:

QT       += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test2
TEMPLATE = app
SOURCES += main.cpp\
        mainwindow.cpp \
    glwidget.cpp
HEADERS  += mainwindow.h \
    glwidget.h
FORMS    += mainwindow.ui

The glwidget.h is the next:

#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
class GLWidget : public QGLWidget
{
    Q_OBJECT
public:
    explicit GLWidget(QWidget *parent = 0);
    void initializeGL();    
};
#endif // GLWIDGET_H

The glwidget.cpp is the next:

#include "glwidget.h"
GLWidget::GLWidget(QWidget *parent) :
    QGLWidget(parent)
{
}
void GLWidget::initializeGL(){
    glClearColor(1.0,1.0,0.0,1.0);
}

The main.cpp:

#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

I have checked that in the *.pro I have included opengl: QT += core gui opengl In addition, I have deleted the "YourProjectName-build-desktop" folder created by QT Creator and build again without success.

The error is: C:\test2\glwidget.cpp:9: error: undefined reference to `_imp__glClearColor@16' where line 9 is glClearColor(1.0,1.0,0.0,1.0);

Which extra step I am missing?

Thank you in advance for your help

Cheers © 2016 Microsoft Terms Privacy & cookies Developers English (United States)

like image 496
John Barton Avatar asked Jan 10 '16 00:01

John Barton


1 Answers

try adding LIBS += -lOpengl32 to .pro file

and if you`re using qt 5 you might as well take this route

QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glClearColor(1.0f, 1.0f, 0.0f, 1.0f);

http://doc.qt.io/qt-5/qopenglwidget.html http://doc.qt.io/qt-5/qopenglcontext.html

EDIT:

just tested this an it works. but requires qt5. Legacy functions seem to be defined in qt 5 so i left out QOpenGLFunctions.

#include <QOpenGLWidget>

class GLWidget : public QOpenGLWidget
{
public:
    GLWidget(QWidget* parent) :
        QOpenGLWidget(parent)
    {

    }

protected:
    void initializeGL()
    {
        glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
    }

    void paintGL()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glColor3f(1,0,0);
        glBegin(GL_TRIANGLES);
        glVertex3f(-0.5, -0.5, 0);
        glVertex3f( 0.5, -0.5, 0);
        glVertex3f( 0.0, 0.5, 0);
        glEnd();
    }

    void resizeGL(int w, int h)
    {
        glViewport(0, 0, w, h);
    }
};
like image 132
DevGuy Avatar answered Nov 08 '22 00:11

DevGuy