quick QImage/QPixmap redisplay of changed buffer
-
I'm new to Qt and Pyside, but trying pyside6 for making a GUI for some C code I'm writing for research. One of the C functions outputs an 8-bit RGB image, in a struct we'll call "foo", with fields "void *data", and "size_t size" for a pointer to the underlying image data buffer and the buffer size, respectively, and fields "unsigned int sx" and "unsigned int sy" for the numbers of columns and rows respectively. I'm calling this code from python via CFFI, and all that is working fine.
I am also successfully display the image data on the screen with something like (based on https://www.pythonguis.com/faq/adding-images-to-pyqt5-applications/ ):
# ... from CFFI thing import ffi # ... initialize foo struct with image data class MyWindow(QMainWindow): def __init__(self, foo): super(MyWindow, self).__init__() self.title = "Image Viewer" self.setWindowTitle(self.title) self.label = QLabel(self) self.buff = ffi.buffer(foo.data, foo.size) self.image = QImage(self.buff, foo.sx, foo.sy, 3*foo.sx, QImage.Format_RGB888) # can modify foo.data here self.pixmap = QPixmap.fromImage(self.image) # cannot modify foo.data here self.label.setPixmap(self.pixmap) self.setCentralWidget(self.label) self.resize(self.pixmap.width(), self.pixmap.height())
However, the purpose of the C library is to quickly change the contents of the image based on other user interactions.
So: what is the absolute fastest way to redisplay the same pixel buffer (with updated contents)? Mainly I want to minimize the number of re-allocations, and maximize the re-use of existing allocations and objects. My C library will just write new values into the same foo.data buffer (no re-allocation), and the required size of the buffer won't change during the lifetime of MyWindow.
I noticed that I can modify foo.data after I create the QImage; it seems to merely wrap rather than copy the existing buffer. But once the QPixmap is created via QPixmap.fromImage(); modifying foo.data does nothing (which it makes sense if QPixmap maintains its own internal buffer).
Is there way to tell the QPixmap: "don't allocate anything, just update the values in your internal buffer from the same place you updated them last time", and, to tell the QLabel: "same QPixMap python object, but new pixel contents, please redisplay"?
Is QPixmap.fromImageInPlace useful here? I can't find documentation that explains its role.
Or is there some other very different way of going from my own pixel buffer to something that Pyside displays, that will facilitate the quick re-displays I'm describing?
Thank you for any tips!
(and sorry for reduplication of question asked on pyside email list)