Answer the question
In order to leave comments, you need to log in
How to deal with assertion in Qt OpenGL?
I rewrote the example from the Qt guide, but it doesn't work.
Runs but issues an Assert and terminates forcibly.
ASSERT: "QOpenGLFunctions::isInitialized(d_ptr)" in file D:\Qt\5.13.2\mingw73_64\include/QtGui/qopenglfunctions.h, line 1092
inline void QOpenGLFunctions::glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
{
if defined(QT_OPENGL_ES_2) && defined(Q_OS_ANDROID)
::glViewport(x, y, width, height);
else
Q_ASSERT(QOpenGLFunctions::isInitialized(d_ptr)); // <--
d_ptr->f.Viewport(x, y, width, height);
#endif
Q_OPENGL_FUNCTIONS_DEBUG
}
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
LIBS += -lopengl32
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.resize(200,200);
w.show();
return a.exec();
}
#pragma once
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
class MainWindow : public QOpenGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
protected:
virtual void initializeGL ( );
virtual void resizeGL (int nWidth, int nHeight);
virtual void paintGL ( );
public:
MainWindow(QWidget *parent = nullptr);
};
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent /* = 0*/)
: QOpenGLWidget(parent), QOpenGLFunctions()
{
QOpenGLFunctions::initializeOpenGLFunctions();
}
/*virtual*/ void MainWindow::initializeGL()
{
QOpenGLFunctions* pFunc =
QOpenGLContext::currentContext()->functions();
pFunc->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
/*virtual*/ void MainWindow::resizeGL(int nWidth, int nHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, (GLsizei)nWidth, (GLsizei)nHeight);
glOrtho(0, 100, 100, 0, -1, 1);
}
/*virtual*/ void MainWindow::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(1, 0, 0);
glVertex2f(0, 100);
glColor3f(0, 1, 0);
glVertex2f(100, 100);
glColor3f(0, 0, 1);
glVertex2f(100, 0);
glColor3f(1, 1, 1);
glVertex2f(0, 0);
glEnd();
}
Answer the question
In order to leave comments, you need to log in
Perhaps you missed something during the rewrite, the author missed something, or something changed in Qt. Try running the appropriate example that comes with QtCreator, such as "OpenGL Window Example" or "Hello GL2 Example".
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question