0

The following is the code for my widget which subclasses QOpenGLWidget. What should be happening is that the framebuffer object flashes between white and black between each frame, but it only displays white, which tells me it's rendering once and then never updating.

void GLWidget::paintEvent(QPaintEvent *event)
{
    QRect sourceRect(0, 0, 320, 240);
    QSize sourceSize = sourceRect.size();
    QOpenGLFramebufferObject sourceBuffer(sourceSize);
    sourceBuffer.bind();
    QOpenGLPaintDevice device(sourceSize);
    QPainter sourcePainter;
    sourcePainter.begin(&device);
    static bool flash = true;
    if (flash)
    {
        sourcePainter.fillRect(sourceRect, QBrush(Qt::white));
    }
    else
    {
        sourcePainter.fillRect(sourceRect, QBrush(Qt::black));
    }
    flash = !flash;
    sourcePainter.end();
    sourceBuffer.release();

    QPainter destinationPainter;
    destinationPainter.begin(this);
    destinationPainter.setRenderHint(QPainter::Antialiasing);
    destinationPainter.fillRect(event->rect(), QBrush(Qt::black));
    int destinationWidth = event->rect().width();
    int destinationHeight = event->rect().height();
    QImage sourceImage = sourceBuffer.toImage();
    //sourceImage.save(&process, "bmp");
    QImage scaledSourceImage = sourceImage.scaled(destinationWidth, destinationHeight, Qt::KeepAspectRatio);
    int translationX = destinationWidth - scaledSourceImage.width();
    int translationY = destinationHeight - scaledSourceImage.height();
    destinationPainter.translate(translationX / 2, translationY / 2);
    destinationPainter.drawImage(0, 0, scaledSourceImage);
    destinationPainter.end();
}

Am I using the framebuffer object correctly here? Also is there a more appropriate way to draw something offscreen and then display it in a QOpenGLWidget?

rogueyoshi
  • 57
  • 8
  • Why going through a temporary FBO instead of just painting on "this"? – peppe Aug 16 '16 at 09:55
  • my project needs to be able to paint to a smaller offscreen buffer first and then resize and display it on the widget. – rogueyoshi Aug 16 '16 at 18:12
  • After `sourceBuffer.release();` it's lost, I believe. Don't need to do that, because the `sourceBuffer` will be deleted anyway. – Velkan Aug 29 '16 at 13:04

0 Answers0