[SOLVED] Calling OpenGL functions in a Qt program (Qt 5.4)



  • I have two classes: glWidget and glWindow. I want to be able to display anything rendered by OpenGL within that widget which is within the window. It does work, but I need access to OpenGL functions later than 4.0+. Here's what I've done:

    @class glWidget : public QOpenGLWidget, public QOpenGLFunctions_4_3_Core
    {
    public:
    // etc..

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

    private:
    QOpenGLContext *context;
    };@

    In the constructor of glWidget, I then do:

    @
    context = versionFunctions<QOpenGLFunctions_4_3_Core>();
    initializeOpenGLFunctions();
    @

    Then within the three inherited functions I do all the OpenGL code. As a preference, I prefer to not use QGLShader, QGLShaderProgram, QGLBuffer, QGLVertexArrayObject, and all the other QGL stuff. I prefer to do things from scratch; but I only want Qt to allow me to use Qt widgets within the window while being able to view what is rendered on screen; that's a requirement.

    Compiling is flawless; but when I run Qt automatically crashes. When I debug, it only points to glClearColor(..) causing the segmentation fault. It would be something I'm doing wrong here. I checked glWindow, and here's the class declaration:

    @class GLWindow : public QMainWindow
    {
    Q_OBJECT

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

    private slots:

    void on_pushButton_clicked();
    

    private:
    Ui::GLWindow *ui;
    };@

    I performed the design and layout of the window through Qt Designer; so the widget therein is promoted to glWidget. But, my problem still remains; I need to find a way to be able to call OpenGL 4.0+ functions within Qt without using glew. Using Qt 5.4, I'd like to be given some insight as to the proper way of being able to call OpenGL functions and do the rendering within the manner I've described above. This is the sort of result I intend to program as a result:

    !http://oi61.tinypic.com/2dj2qf4.jpg(glWidget result)!

    As you can see, I have a widget that renders OpenGL stuff, and a Qt push button that quits the window. How I rendered the triangle was through immediate mode; but if I am to be using shaders and the latest OpenGL functionality, how can I achieve this without using glew and through Qt's own classes? I know that QOpenGLFunctions_4_3_Core has the functions I need, but now I need to know I can prevent Qt from crashing when running my program with them. Sorry for the wordy question! I'm happy to elaborate.



  • [quote author="Vormeph" date="1422636716"]
    @
    context = versionFunctions<QOpenGLFunctions_4_3_Core>();
    @
    [/quote]

    What's that?

    You don't need a separate QOpenGLContext. Just call initializeOpenGLFunctions() and that's it.



  • [quote author="agocs" date="1422879634"]
    [quote author="Vormeph" date="1422636716"]
    @
    context = versionFunctions<QOpenGLFunctions_4_3_Core>();
    @
    [/quote]

    What's that?

    You don't need a separate QOpenGLContext. Just call initializeOpenGLFunctions() and that's it.
    [/quote]

    Thanks for your reply. I have solved my issue!



  • Additionally, you need to request an OpenGL 4.3 (or newer) core profile context by calling setFormat() in the QOpenGLWidget subclass' constructor:

    [code]
    GLWindow::GLWindow() {
    QSurfaceFormat format;
    format.setVersion(4, 3);
    format.setProfile(QSurfaceFormat::CoreProfile);
    setFormat(format);
    }
    [/code]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.