I need to draw pixel data that is being held by a library as uint8_t * and that is updated frequently and partially. I get a call-back from the library every time an update is done, that looks like this:
void gotFrameBufferUpdate(int x, int y, int w, int h);
I've tried creating a QImage using the pixel data pointer
QImage bufferImage(frameBuffer, width, height, QImage::Format_RGBX8888);
and let the call-back trigger update() of my widget
void gotFrameBufferUpdate(int x, int y, int w, int h)
{
update(QRect(QPoint(x, y), QSize(w, h)));
}
which simply draws the updated area of the QImage via paint():
void MyWidget::paint(QPainter *painter)
{
QRect rect = painter->clipBoundingRect().toRect();
painter->drawImage(rect, bufferImage, rect);
}
The problem with this approach is that the QImage does not seem to reflect any updates to the pixel buffer. It keeps showing its initial contents.
My current workaround is to re-create a QImage instance each time the buffer is updated:
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)));
}
This works but seems very inefficient to me. Is there a better way of dealing with externally updated pixel data in Qt? Can I make my QImage aware of updates to its memory buffer?
(Background: I'm writing a custom QML type with C++ backend that shall display the contents of a VNC session. I'm using LibVNC/libvncclient for this.)
The QImage is updated if one calls QImage::bits().
It does not allocate a new buffer, you can discard the result, but it magically triggers a refresh of the image. It is required every time you want a refresh.
I don't know if this is guaranteed behaviour, nor if it saves anything over recreating it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With