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)
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);
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With