Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: How to make QImage aware of updated memory buffer?

Tags:

c++

qt

vnc-viewer

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.)

like image 748
akoch Avatar asked Oct 29 '25 17:10

akoch


1 Answers

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.

like image 99
Andrea Avatar answered Oct 31 '25 05:10

Andrea