I have an RGB888 format qImage defined as follows:
int sizeX = 300; int sizeY = 300;
QImage img = QImage(sizeX, sizeY, QImage::Format_RGB888);
I wish to print current pixel of img one by one. So, I followed the example here:`
for(int i=0; i<sizeX; i++){
for(int j=0; j<sizeY; j++){
img.setPixel(i, j, qRgb(rand()%256, rand()%256, rand()%256));
}
}
QGraphicsScene *graphic = new QGraphicsScene(this);
graphic->addPixmap(QPixmap::fromImage(img));
ui->graphicsView->setScene(graphic);
But it prints the whole QImage. I want to print one by one. Thanks. Best regards.
As JarMan commented, you are looking for "animation".
Drawing pixel by pixel, and updating the UI for every pixel, may use animation solution as described in the following post.
In Qt5 we have to execute
QtWidgets.QApplication.processEvents()
for forcing Qt to redraw.Note:
I implemented the code in Python (with PyQt5), the syntax is different from C++, but the concept is the same.
Initialization stage:
QGraphicsView
class.graphic = QGraphicsScene(0, 0, sizeX, sizeY)
pixmap = QPixmap.fromImage(img)
graphic
object, and keep the returned reference:pixmap_item = graphic.addPixmap(pixmap)
setScene(graphic)
Animation:
In the animation loop, we have to execute
setPixmap
on every iteration for updating the entire image.(The is a way for drawing pixel by pixel, but it's a deviation from your question).
Simplified code for updating the image (executed each iteration):
Complete Python code sample:
I hope it's not too much Python for you...
Sample output:
