欣然接受';t在Qt小部件中初始化

欣然接受';t在Qt小部件中初始化,qt,opengl,glad,Qt,Opengl,Glad,我正在尝试在Qt中创建一个自定义渲染小部件。这个例子是OpenGL,但最终我想添加DirectX。我遇到的问题是GLAD没有初始化(gladLoadGL返回false)。我的操作系统是Linux(ubuntu),构建系统是cmake。当我构建并运行应用程序时,会打开一个完全黑色的窗口。终端只是打印出“失败”,表示没有加载 这是我的密码 // viewport.h class Viewport : public QWidget { Q_OBJECT; private: int i

我正在尝试在Qt中创建一个自定义渲染小部件。这个例子是OpenGL,但最终我想添加DirectX。我遇到的问题是GLAD没有初始化(gladLoadGL返回false)。我的操作系统是Linux(ubuntu),构建系统是cmake。当我构建并运行应用程序时,会打开一个完全黑色的窗口。终端只是打印出“失败”,表示没有加载

这是我的密码

// viewport.h
class Viewport : public QWidget
{
    Q_OBJECT;

private:
    int init = false;

protected:
    virtual void paintEvent(QPaintEvent* paintEvent) override;
    virtual void showEvent(QShowEvent* showEvent) override;

public:
    Viewport(QWidget* parent = nullptr);
    virtual QPaintEngine* paintEngine() const override;
    void initGL();
};



// viewport.cpp

#include <glad/glad.h>
#include "viewport.h"
#include <QGridLayout>
#include <QLabel>
#include <QWidget>
#include <QResizeEvent>
#include <QApplication>
#include <iostream>

Viewport::Viewport(QWidget* parent) : QWidget(parent)
{
    setAttribute(Qt::WA_NativeWindow);
    setAttribute(Qt::WA_PaintOnScreen);
    setAttribute(Qt::WA_NoSystemBackground);
}

void Viewport::initGL()
{
    auto nativeWindowHandler = winId();
    if(gladLoadGL()) {
        std::cout << "initialized" << std::endl;
        init = true;
    }
    else
    {
        std::cout << "failed" << std::endl;
    }
}

QPaintEngine* Viewport::paintEngine() const
{
    return nullptr;
}

void Viewport::paintEvent(QPaintEvent* paintEvent)
{
    if(init == false)
    {
        initGL();
    }
}

void Viewport::showEvent(QShowEvent* showEvent)
{
    QWidget::showEvent(showEvent);
    if(init == false)
    {
        initGL();
    }
}
//viewport.h
类视口:公共QWidget
{
Q_对象;
私人:
int init=false;
受保护的:
虚拟void paintEvent(QPaintEvent*paintEvent)覆盖;
虚拟void showEvent(QShowEvent*showEvent)覆盖;
公众:
视口(QWidget*parent=nullptr);
虚拟QPaintEngine*paintEngine()常量覆盖;
void initGL();
};
//viewport.cpp
#包括
#包括“viewport.h”
#包括
#包括
#包括
#包括
#包括
#包括
视口::视口(QWidget*父对象):QWidget(父对象)
{
setAttribute(Qt::WA_NativeWindow);
setAttribute(Qt::WA_画屏);
setAttribute(Qt::WA_NoSystemBackground);
}
void Viewport::initGL()
{
auto-nativeWindowHandler=winId();
if(gladLoadGL()){
标准::cout