Q:

load image from char buffer

I need to construct an image with unsigned char data I receive from a compressed/decompressed image. For this, I just wrote a simple program to test buffer loading from image and vice versa. As I run the code, I cannot setpixmap the image to the background. 
void MainWindow::LoadImage()
{
    //======== Load buffer from image
    unsigned char buffer[_width*_height*COLOR_COMPONENTS]; //1024 * 768 * 3
    QImage image;
    image.load("://image.jpg", "JPEG");
    memcpy(buffer, image.bits(), _width*_height*COLOR_COMPONENTS);

    //========= Load image from buffer
    QImage img;
    img.loadFromData((const char*)buffer);
    QPixmap px = QPixmap::fromImage(img);
    ui->label->setPixmap(px);
}

UPDATED:
I changed the code to this, however, I get segmentation fault with memcpy.

unsigned char buffer[400*300*3];

QImage image(_width, _height, QImage::Format_RGB32);

image.load("://image.jpg", "JPEG");

memcpy(buffer, image.bits(), 400*300*3);

QImage img(400, 300, QImage::Format_RGB32);

img.loadFromData((const uchar*)buffer, sizeof(buffer)/sizeof(char), "JPG");

QPixmap px = QPixmap::fromImage(img);

ui->label->setPixmap(px);

A:

loadFromData requires the data to be in a certain format (PNG, JPG...), the plain array is not a valid one
however doing 
QImage img(buffer,_width, _height, QImage::Format_RGB888);

will return a image of the correct size and format but will only be valid for as long as buffer is alive

