Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt5 opengl widget givng error openglfunctions line 656



  • hey guys
    using windows 10 and qt creator qt 5
    im trying to out a opengl 3 widget on qt I have looked at some youtube tutorials and such but it is giving me this error when using qt5 with openglwidget using a widget on the ui

    main.cpp

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

    glwidget.cpp

    #include "glwidget.h"
    
    glwidget::glwidget(QWidget *parent):QOpenGLWidget {parent}
    {
    
    }
    
    void glwidget::initializeGL()
    {
        glClearColor(0,0,0,1);
        initializeOpenGLFunctions();
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHT0);
        glEnable(GL_LIGHTING);
        glEnable(GL_COLOR_MATERIAL);
    
    }
    
    void glwidget::paintGL()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    
    
    
    
    }
    
    void glwidget::resizeGL(int w, int h)
    {
    
    
    
    
    
    
    }
    
    void glwidget::qColorToRGB(const QColor &C, float &r, float &g, float &b) const
    {
    
    
    
    
    
    }
    
    float glwidget::normalize_0_1(float val,float min,float max) const
    {
    
    
    
        return (val - min) / (max - min);
    }
    

    glwidget.h

    #ifndef GLWIDGET_H
    #define GLWIDGET_H
    
    #include <QGLWidget>
    #include <QOpenGLWidget>
    #include <QOpenGLFunctions>
    #include <QProcess>
    
    class glwidget : public QOpenGLWidget,public QOpenGLFunctions
    {
      //  Q_OBJECT
    public:
        explicit glwidget(QWidget *parent = nullptr);
    protected:
        void initializeGL();
        void paintGL() override;
        void resizeGL(int w,int h) override;
    private:
        void qColorToRGB(const QColor &C, float &r,float &g,float &b) const;
        float normalize_0_1(float val,float min,float max) const;
    };
    
    
    #endif // GLWIDGET_H
    

    Mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    
    Mainwindow.cpp 
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    i have promoted

    screenshot of error

    qtopenglnotworking.PNG ![alt text](image url)


  • Moderators

    void glwidget::initializeGL()
    {
        glClearColor(0,0,0,1);       // <--- HERE
        initializeOpenGLFunctions();
        ...
    

    You are using a GL function before a pointer to it was resolved. Don't call any glXXX functions before initializeOpenGLFunctions(). Just switch those two lines and you should be good to go.


  • Lifetime Qt Champion

    Hi,

    The code in your image does not seem to match what you posted.

    It's missing the initializeOpenGLFunctions(); call in your initializeGL override.



  • ah theres a myglwidget that i tried using from a second youtube video tutorial but i went back to glwidget.h/cpp which was the original tutorial the myglwidget.h/cpp file in the image can be ignored


  • Moderators

    void glwidget::initializeGL()
    {
        glClearColor(0,0,0,1);       // <--- HERE
        initializeOpenGLFunctions();
        ...
    

    You are using a GL function before a pointer to it was resolved. Don't call any glXXX functions before initializeOpenGLFunctions(). Just switch those two lines and you should be good to go.



  • @Chris-Kawa ah ok that worked and learned something new thanks