【问题标题】:Problems with OpenGL when running QT Creator运行 QT Creator 时 OpenGL 的问题
【发布时间】:2016-01-10 00:13:37
【问题描述】:

我正在尝试运行 OpenGL 的基本示例,使用 QT Creator 为窗口提供颜色。但是,在调用 OpenGL 指令时,我在编译中遇到错误: glClearColor(1.0,1.0,0.0,1.0); *.pro 文件是下一个:

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

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 是下一个:

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

main.cpp:

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

我已经在 *.pro 中检查过,我已经包含了 opengl: QT += core gui opengl 另外,我把QT Creator创建的“YourProjectName-build-desktop”文件夹删除了,再次build没有成功。

错误是: C:\test2\glwidget.cpp:9: 错误:未定义的对 `_imp__glClearColor@16' 的引用 其中第 9 行是 glClearColor(1.0,1.0,0.0,1.0);

我缺少哪个额外步骤?

提前感谢您的帮助

干杯 © 2016 Microsoft 条款隐私和 cookie 开发人员英语(美国)

【问题讨论】:

  • 你得到哪个错误?
  • 我得到 C:\test2\glwidget.cpp:9: error: undefined reference to `_imp__glClearColor@16' 其中第 9 行是 glClearColor(1.0,1.0,0.0,1.0);

标签: c++ qt opengl opengl-es


【解决方案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

编辑:

刚刚测试了它,它可以工作。但需要qt5。 遗留函数似乎是在 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);
    }
};

【讨论】:

  • 谢谢,它适用于初始化GL。在这个函数之后,我有 paintGL 函数(我没有发布),其中包括: glClear(GL_COLOR_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() 我已经应用了相同的更改,但没有工作。
  • @ArturoReyesLopez 我添加了一些需要 qt 5 的测试代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多