QOpenGLWindow - paintGL is never called
-
Hello,
I'm learning with new Qt classes like VAO, VBO, OpenGLWindow..
I took example and little bit rewrite it. Because example wasn't using paintGL but QPaintDevice.
Currently I have following code:
#include "glwindow.h" #include <QCoreApplication> #include <QDebug> GLWindow::GLWindow() : QOpenGLWindow(QOpenGLWindow::NoPartialUpdate) { } GLWindow::~GLWindow() { } void GLWindow::initializeGL() { initializeOpenGLFunctions(); connect(this, &GLWindow::frameSwapped, this, &GLWindow::renderNow); // use this or last commented out section in renderNow()? glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); initialize(); renderNow(); } void GLWindow::paintGL() { qDebug() << "c"; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); render(); painted = true; } void GLWindow::renderNow() { if(!isExposed()) return; /*if(!painted) // useless to continue when paint wasnt done yet return; painted = false;*/ QOpenGLContext::currentContext()->makeCurrent(this); QOpenGLWindow::update(); // <---- this not calling paintGL at all :(( QOpenGLContext::currentContext()->swapBuffers(this); qDebug() << "draw"; // use connect or this? what is faster? /*if(draw && !pendingUpdate) { pendingUpdate = false; QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest)); }*/ } void GLWindow::resizeGL(int width, int height) { const qreal pixelRatio = devicePixelRatio(); glViewport(0, 0, width * pixelRatio, height * pixelRatio); } bool GLWindow::event(QEvent* event) { switch(event->type()) { case QEvent::UpdateRequest: { pendingUpdate = false; renderNow(); return true; } default: return QWindow::event(event); } } void GLWindow::exposeEvent(QExposeEvent* event) { Q_UNUSED(event); if(isExposed()) renderNow(); } void GLWindow::initialize() // virtual { } void GLWindow::render() // virtual { }This should be class which will be inherited.
What's the problem? I get from qDebug only "draw" not "paintGL" or "c". PaintGL should be called from line "QOpenGLWindow::update(); // <---- this not calling paintGL at all :((" or I had to missunderstand documentations.
Also last question, I did some comments to code, is better to use connect to repaint or QCoreApplication::postEvent?
Thanks
-
Ok I figured it out, there is broken code in example.
bool GLWindow::event(QEvent* event) { switch(event->type()) { case QEvent::UpdateRequest: { pendingUpdate = false; renderNow(); return true; } default: return QWindow::event(event); } }Should be
bool GLWindow::event(QEvent* event) { if(event->type() == QEvent::UpdateRequest) { pendingUpdate = false; renderNow(); } return QWindow::event(event); }So there is only question which remains, what's better to use as update? PostEvent or connect?
Thanks
EDIT: next question, why is QOpenGLWindow limited to 30 frames per second? How to make 60fps?
EDIT 2: fps solved with setSwapInterval(0)
EDIT 3: I guess to FPS counter that QCoreApplication::PostEvent is 5 times faster than connect, but still give me your advice