我需要绘制由库保存的像素数据,这些数据uint8_t *
经常和部分更新。每次更新完成时,我都会从库中收到回调,如下所示:
void gotFrameBufferUpdate(int x, int y, int w, int h);
我尝试使用像素数据指针创建QImage
QImage bufferImage(frameBuffer, width, height, QImage::Format_RGBX8888);
update()
并让我的小部件的回调触发
void gotFrameBufferUpdate(int x, int y, int w, int h)
{
update(QRect(QPoint(x, y), QSize(w, h)));
}
它只是通过以下方式绘制QImagepaint()
的更新区域:
void MyWidget::paint(QPainter *painter)
{
QRect rect = painter->clipBoundingRect().toRect();
painter->drawImage(rect, bufferImage, rect);
}
这种方法的问题是QImage似乎没有反映对像素缓冲区的任何更新。它不断显示其初始内容。
我当前的解决方法是每次更新缓冲区时重新创建一个QImage实例:
void gotFrameBufferUpdate(int x, int y, int w, int h)
{
if (bufferImage)
delete bufferImage;
bufferImage = new QImage(frameBuffer, width, height,
QImage::Format_RGBX8888);
update(QRect(QPoint(x, y), QSize(w, h)));
}
这可行,但对我来说似乎效率很低。有没有更好的方法来处理 Qt 中外部更新的像素数据?我可以让我的QImage知道其内存缓冲区的更新吗?
(背景:我正在使用 C++ 后端编写一个自定义 QML 类型,该类型应显示 VNC 会话的内容。我为此使用LibVNC/libvncclient。)