myopenglwidget.h
#ifndef MYOPENGLWIDGET_H
#define MYOPENGLWIDGET_H
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLFunctions_4_0_Core>
#include <QOpenGLFunctions_4_1_Core>
#include <QOpenGLFunctions_4_2_Core>
#include <QOpenGLFunctions_4_3_Core>
#include <QOpenGLFunctions_4_4_Core>
#include <QOpenGLFunctions_4_5_Core>
#include <QPen>
class myOpenGLWidget : public QOpenGLWidget, public QOpenGLFunctions_4_5_Core
{
Q_OBJECT
public:
explicit myOpenGLWidget(QWidget *parent = nullptr);
protected:
void initializeGL() Q_DECL_OVERRIDE;
void paintEvent(QPaintEvent *e) Q_DECL_OVERRIDE;
void paintGL() Q_DECL_OVERRIDE;
private:
QPen mPen;
};
#endif // MYOPENGLWIDGET_H
myopenglwidget.cpp
#include "myopenglwidget.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>
myOpenGLWidget::myOpenGLWidget(QWidget *parent) : QOpenGLWidget(parent)
{
mPen.setWidth(5);
mPen.setColor(Qt::green);
}
void myOpenGLWidget::initializeGL()
{
#if 0
QOpenGLFunctions_4_5_Core* funcs = nullptr;
funcs = context()->versionFunctions<QOpenGLFunctions_4_5_Core>();
if (!funcs) {
qWarning() << "Could not obtain required OpenGL context version";
exit(1);
}
funcs->glClearColor(1.0f, 1.0f, 0.0f, 0.0f);
#else
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glClearColor(0.0f, 0.5f, 0.5f, 0.0f);
#endif
}
void myOpenGLWidget::paintEvent(QPaintEvent *e)
{
QOpenGLWidget::paintEvent(e);
QPainter painter(this);
painter.setPen(mPen);
painter.drawText(100, 100, "hello");
}
void myOpenGLWidget::paintGL()
{
QOpenGLWidget::paintGL();
#if 0
QOpenGLFunctions_4_5_Core* funcs = nullptr;
funcs = context()->versionFunctions<QOpenGLFunctions_4_5_Core>();
if (!funcs) {
qWarning() << "Could not obtain required OpenGL context version";
exit(1);
}
funcs->glClear(GL_COLOR_BUFFER_BIT);
#else
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glClear(GL_COLOR_BUFFER_BIT);
#endif
}