关于QOpenGLWidget的错误

时间:2016-05-07 03:10:42

标签: opengl qt5

How do I render a triangle in QOpenGLWidget? 当我做链接时,会发生错误 errors like these mywidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>

namespace Ui {
class myWidget;
}

class myWidget : public QWidget
{
    Q_OBJECT

public:
    explicit myWidget(QWidget *parent = 0);
    ~myWidget();

private:
    Ui::myWidget *ui;
};

#endif // MYWIDGET_H

mywidget.cpp

#include "mywidget.h"
#include "ui_mywidget.h"

myWidget::myWidget(QWidget *parent) :
    QWidget(parent),
ui(new Ui::myWidget)
{
    ui->setupUi(this);
}

myWidget::~myWidget()
{
    delete ui;
}

oglwidget.h

#ifndef OGLWIDGET_H
#define OGLWIDGET_H
#include <QWidget>
#include <QOpenGLWidget>
#include <GL/glu.h>
#include <GL/gl.h>

class OGLWidget : public QOpenGLWidget
{
public:
    OGLWidget(QWidget *parent=0);
    ~OGLWidget();
protected:
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();
};

#endif // OGLWIDGET_H

oglwidget.cpp

#include "oglwidget.h"

OGLWidget::OGLWidget(QWidget *parent)
    :QOpenGLWidget(parent)
{

}

OGLWidget::~OGLWidget()
{

}

void OGLWidget::initializeGL()
{
    glClearColor(0,0,0,1);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    glEnable(GL_COLOR_MATERIAL);
}

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

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

void OGLWidget::resizeGL(int w, int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, (float)w/h, 0.01, 100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,5,0,0,0,0,1,0);
}

的main.cpp

#include "mywidget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    myWidget w;
    w.show();

    return a.exec();
}

form 为什么会发生错误?我错过了一些包含文件吗? Qt版本5.5

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案 最后两行应添加到.pro文件

HEADERS       = glwidget.h \
            mainwindow.h
SOURCES       = glwidget.cpp \
            main.cpp \
            mainwindow.cpp
QT           += opengl
LIBS        +=  -lopengl32
LIBS        += -lglu32

然后qtcreator_process_stub终端窗口可能会显示,我转到project-&gt; run-&gt;取消选中“Run in terminal” 是的,那很有效!我的操作系统是Win10 64bit和Qt5.6 with mingw