Search code examples
qtqt4qt4.7qt4.6qt4.8

How to draw pixels from display buffer?


Is there any example Qt code which displays the image from an unsigned char display buffer? Each byte on the buffer corresponds to the Gray scale pixel color. The content of the display buffer changes at run time in specified intervals. I need to change the display buffer content as fast as I can, so that the image seems to be moving. My question is how to draw pixels from the buffer very fast? I don’t need to save the image, just want to display it. Please help


Solution

  • QImage provides a constructor for initialization using an unsigned char buffer. In order to display it you could use a QGraphicsView with a QGraphicsScene. Every time the data of the buffer changes you could call a slot similar to the following one:

    void updateImage()
    {
         // I assume an 1024x768 image
         QImage img(buffer, 1024, 768, QImage::Format_Indexed8);
         scene->clear();
         scene->addPixmap(QPixmap::fromImage(img));
         graphicsView->update(); 
    }
    

    You could also use the QPixmap's loadFromData in order to load directly the pixmap from the unsigned char array and avoid the QImage step.