Problems with OpenGL when starting QT Creator

I am trying to run basic OpenGL examples using QT Creator to give color to a window. However, I get a compilation error when invoking the OpenGL instruction: glClearColor (1.0,1.0,0.0,1,0); The * .pro file is as follows:

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

Next is glwidget.h:

#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

glwidget.cpp is as follows:

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

Main.cpp file:

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

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

Error: 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);

What extra step am I missing?

Thank you in advance for your help.

© 2016 Microsoft cookie English (United States)

+4
1

LIBS += -lOpengl32 .pro

qt 5,

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:

, . qt5. Legacy, -, qt 5, 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);
    }
};
+5

Source: https://habr.com/ru/post/1623607/


All Articles