OpenGLWidget in QT

1. In the generated UI, arrange the corresponding space through the control OpenGL Widget (later, bind this control to the derived OpenGLWidget through the promoted widget)

 

2. Add a widget class, which is derived from QOpenGLWidget, QOpenGLFunctions_*_*_Core (* represents the version number)

Classes inherited from QOpenGLWidget and QOpenGLFunctions_*_*_Core need to overload virtual void initializeGL(); virtual void resizeGL(int w, int h); virtual void paintGL(); three functions

#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
class AXBOpenGLWidget : public QOpenGLWidget, QOpenGLFunctions_3_3_Core

Overload initializeGL(), paintGL() and resizeGL(int w, int h)

#ifndef AXBOPENGLWIDGET_H
#define AXBOPENGLWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>

class AXBOpenGLWidget : public QOpenGLWidget, QOpenGLFunctions_3_3_Core
{
    Q_OBJECT
public:
    explicit AXBOpenGLWidget(QWidget *parent = nullptr);

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

signals:

};

#endif // AXBOPENGLWIDGET_H

Do some basic initialization in the initializeGL() function

First implement a simple modification of the background function

#include "axbopenglwidget.h"
AXBOpenGLWidget::AXBOpenGLWidget(QWidget *parent) : QOpenGLWidget(parent)
{
}
void AXBOpenGLWidget::initializeGL()
{
initializeOpenGLFunctions();
}
void AXBOpenGLWidget::resizeGL(int w, int h)
{
//glViewport(0,0,w,h);
}
void AXBOpenGLWidget::paintGL()
{
glClearColor(0.2f,0.3f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}

 Renderings:

 

Note 1:

If initializeOpenGLFunctions() is not called in initializeGL ; the following problems will occur

 

2. If you don't want the yellow triangles such as unused parameter 'w' to appear, you can use Q_UNUSED() to avoid it

Q_UNUSED(w)
Q_UNUSED(h)

3. Turning off the yellow warning on the right can also be done in the following way

Click Qt Cretor in the upper left corner of the screen - about plugins - Name - c++: ClangcodeModel, cancel the checkmark in this column ✔️, then restart Qt Cretor, after opening it again, it will be fine

 

The article is transferred from Blog Garden (unicornsir): OpenGLWidget in QT - unicornsir - Blog Garden

Guess you like

Origin blog.csdn.net/QtCompany/article/details/132119550