There is a really faster way: create an array of unsigned characters and directly change the pixel values. Then create a QImage from this array. Calling setPixel () is very expensive.
unsigned char* buffer_;
buffer_ = new unsigned char[4 * w * h];
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
unsigned char r, g, b;
buffer_[4 * (i * w + j) ] = r;
buffer_[4 * (i * w + j) + 1] = g;
buffer_[4 * (i * w + j) + 2] = b;
}
}
QImage:: format_RGB32 paintEvent() :
void paintEvent(QPaintEvent* event){
QImage image(buffer_, w, h, QImage::Format_RGB32);
painter.drawImage(QPoint(0, 0), image);
}