0

以下是我的小部件的代码,它是 QOpenGLWidget 的子类。应该发生的是帧缓冲区对象在每帧之间在白色和黑色之间闪烁,但它只显示白色,这告诉我它只渲染一次然后从不更新。

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();
}

我在这里正确使用帧缓冲区对象吗?还有一种更合适的方法可以在屏幕外绘制一些东西,然后在 QOpenGLWidget 中显示它吗?

4

0 回答 0