I think the error in your code is about the image data, see QImage constructor:
QImage::QImage ( uchar * data, int width, int height, Format format )
Constructs an image with the given width, height and format, that uses an existing memory buffer, data. The width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.
The buffer must remain valid throughout the life of the QImage. The image does not delete the buffer at destruction.
I think the error in your code is about the image data, see QImage constructor: QImage::QImage ( uchar * data, int width, int height, Format format ) Constructs an image with the given width, height and format, that uses an existing memory buffer, data. The width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned. The buffer must remain valid throughout the life of the QImage. The image does not delete the buffer at destruction.