View Full Version : Please help - Refreshing a QPixmap repeatedly in a for loop ( ??? )

16th June 2011, 18:37
Hello Everyone,

I am manipulating a rectangular array pixel-by-pixel with QImage and setpixel. Next, I convert the QImage to a QPixmap, and display it through a QLabel. This process works pretty well in itself, but, I would like to be able to manipulate the image's pixels over and over, and display the "updated" Qimage (or Qpixmap) file in the label.

Right now, the QLabel remains white/blank until the for.. loop it is in, finishes. Instead, I'd like to see the changed pixmap after each 'step' within the for loop.

Any suggestions? Is there a method to successfully output the QImage in my QLabel after each loop through the step loop? The ideal result would be a Qlabel that displays the Qpixmap which, after each time through the for (step<numbstep) loop, the color is slightly modified via adjusting the R G B values. Right now, it only outputs the "final" color. (Code Below):

#include "widget.h"
#include "ui_widget.h"
#include <QLabel>
#include <iostream>
#include <QImage>
#include <QPixmap>

using namespace std;

Widget::Widget(QWidget *parent) :
ui(new Ui::Widget)

mylabel = new QLabel; //
mylabel->show(); //

myimage = QImage(100,100,QImage::Format_RGB32);

for(int step=0;step<250;step++)
da = 3;db=1;dc=.5;

for(int i=0;i<100;i++)
for(int j=0;j<100;j++)

QRgb colorval;
colorval = qRgb(a,b,c);

//cout << " Setting " << i << " " << j << endl;

a = a + da;
b = b + db;
c = c + dc;

QPixmap mypixmap = QPixmap::fromImage(myimage);



delete ui;

16th June 2011, 19:09
Keeping the GUI Responsive

16th June 2011, 19:59
Okay, thanks, I took a look at your post, and implemented :
QCoreApplication::processEvents(); after setting my pixmap from my Qimage.

Now, I can visually notice the pixmap being updated with a new color, rapidly.

Onto another issue, is there a simple way to delay a step by a given amount of ms or seconds? The pixmap is updated a little too rapidly at this point. It would be nice to be able to control the time the pixmap is displayed.

Right now the only way I can "trick" the program into slowing down is to perform many more steps in my step loop (~20,000 steps) and output my pixmap every 100 steps or so.


16th June 2011, 20:19
Read the article again. Implement the "step by step" approach, just adjust the timer.

16th June 2011, 20:53
My example code that I provided in my first post is a greatly simplified case of what actually occurs in my for loop. When I try to implement
QCoreApplication::processEvents(); in my program that does a lot of calculations within the loop, the program only displays the final step's pixmap in the QLabel. The intermediate pixmaps which I need displayed never show up. Any suggestions?

16th June 2011, 21:56
Any suggestions?
Yes. Don't use processEvents() and redesign your code. Read what the article says on using processEvents(), especially the part starting with "This approach has significant drawbacks". And then read my previous post again and follow the guideline contained in it.

16th June 2011, 21:59
Ok. That is what I did. It works now. Have a nice day.