Qt 5.5 QOpenGLWidget链接错误未链接任何openGL调用 [英] Qt 5.5 QOpenGLWidget link error not linking any openGL calls

查看:874
本文介绍了Qt 5.5 QOpenGLWidget链接错误未链接任何openGL调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Qt 5.5.1构建一个简单的OpenGL应用程序,并且一切正常,直到我尝试使用诸如glClearColor之类的openGL原生函数调用为止.

I tried to build a simple OpenGL App with Qt 5.5.1, and everything is fine up until I try to use openGL native function calls like glClearColor.

该Widget实际上会编译并产生黑屏,但是在我尝试使用任何openGL本机函数后,它甚至没有链接,但是会产生错误:

The Widget actually compiles and produces a black screen, but after I try to use any openGL native function it doesn't even link, but produces the error:

glwidget.cpp:10: error: undefined reference to '_imp__glClearColor@16'

这是.pro文件:

            QT       += core gui opengl

            CONFIG   += windows

            greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

            TARGET = Vehicle_simulation

            TEMPLATE = app

            SOURCES += main.cpp\
                    simulation.cpp \
                glwidget.cpp

            HEADERS  += simulation.h \
                glwidget.h

            FORMS    += simulation.ui

我正在使用Desktop Qt mingw492_32套件. 不过,奇怪的是,我在lib文件夹中找不到libQtOpenGL.so.我的QT安装有问题吗?我尝试多次重新安装它,但没有帮助.在哪里可以下载该特定库?

I'm using Desktop Qt mingw492_32 kit. The strange thing though is that I did not find libQtOpenGL.so in the lib folder. Is my QT installation faulty? I tried to reinstall it multiple times, but it didn't help. Where can I download that specific library?

将其链接到项目可以解决问题,但是我似乎在任何地方都找不到它.

linking it to the project would solve the problem, but I can't seem to find it anywhere.

问题是QT安装中缺少openGL模块,不是我无法链接到它.

The problem is the openGL module is missing from the QT installation, it's not that I am unable to link to it.

推荐答案

我偶然打开了一个示例程序后,只看一下实现.我发现在新的示例程序中已经实现了相同的QOpenGLWidget.

After I opened up an example program by chance, just to take a look at the implementation; I found that the same QOpenGLWidget has been implemented in a new example program.

经过一番分析,我设法找出了问题所在.

After a bit of analysis I managed to figure out the problem.

QT具有内部openGL实现和本机openGL支持.我使用的小部件继承自QOpenGLWidget,但是openGL函数调用(如glClearColor)试图访问Qt中的本机openGL实现.由于那些未包含在我的Qt构建中,因此该项目将不会构建.

QT has an internal openGL implementation and native openGL support as well. The widget I used inherited from QOpenGLWidget, but the openGL function calls (like glClearColor) tried to access native openGL implementation in Qt. which Since those were not included in my Qt build, the project would not build.

要解决此问题,必须运行自定义的Qt核心版本,或使用Qt提供的openGL包装器.

To fix this one has to either run a custom Qt core build, or use the openGL wrapper provided by Qt.

最后我使用了Qt包装器,示例程序

I used the Qt wrapper in the end, which was also used by the example program Cube. the class used to implement the widget (called GLWidget in my implementation) has to inherit not only from QOpenGLWidget, but also from QOpenGLFunctions. The latter part was missing from my implementation. The source code for it is the following:

glwidget.h(初始):

#ifndef GLWIDGET_H
#define GLWIDGET_H


#include <QOpenGLFunctions>
#include <QOpenGLWidget>

class GLWidget : public QOpenGLWidget
{
    Q_OBJECT

public:
    explicit GLWidget(QWidget *parent);

protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int w, int h);
};

#endif // GLWIDGET_H

glwidget.h(固定):

#ifndef GLWIDGET_H
#define GLWIDGET_H


#include <QOpenGLFunctions>
#include <QOpenGLWidget>

class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT

public:
    explicit GLWidget(QWidget *parent);

protected:
    void initializeGL() Q_DECL_OVERRIDE;
    void paintGL() Q_DECL_OVERRIDE;
    void resizeGL(int w, int h) Q_DECL_OVERRIDE;
};

#endif // GLWIDGET_H

小心!

在使用任何gl *函数之前,必须先调用函数initializeOpenGLFunctions();,否则会弹出非常隐秘的运行时错误. 即:

Be careful!

Before any gl* functions are used the function initializeOpenGLFunctions(); has to be called, otherwise a very cryptic run-time error will pop up. i.e.:

void GLWidget::initializeGL(){
    initializeOpenGLFunctions();
    glClearColor(1,0,0,1);
}

希望这对其他人也有帮助.

Hope this will help someone else as well.

这篇关于Qt 5.5 QOpenGLWidget链接错误未链接任何openGL调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆