Important: Please read the Qt Code of Conduct -

QImage::loadFromData QByteArray

  • Hi, I'd need some help with this.
    I have loaded a table from a database in a QTableView
    This table contains some records with images - bitmaps. They are stored as bytes, of course.
    So, I'd like to load and view those images somehow, for example, when I click on some record.

    I wrote these function.

    @ void MainWindow::on_tableView_pressed(const QModelIndex &index)

    QImage *image = new QImage;

    QByteArray barray =;

    image->loadFromData(barray, "BMP");


    Since I've read that the QImage could act as a Buffer.
    But this doesn't work.

    So I use a QBuffer:

    @void MainWindow::on_tableView_pressed(const QModelIndex &index)

     QPixmap *pixmap = new QPixmap;
    QByteArray barray =;
    QBuffer *buf = new QBuffer;
    QGraphicsScene *scene = new QGraphicsScene();


    But this doesn't work either.

    Am I doing something wrong?

    The data was storaged in the database with a code written in Builder, and it was load like this:

    @TByteDynArray Buffer=DataModuleCET->ADOQuery2->FieldByName("Data")->AsBytes;
    TMemoryStream* Str = new TMemoryStream(); if(Buffer.Length>0) Str->Write((unsigned char*)&Buffer[0],Buffer.Length);
    Str->Seek((int)0,(unsigned short)soBeginning);

    Any suggesion in how to reproduce this in Qt C++?

  • Lifetime Qt Champion


    One thing is not clear, in your database, do you store only the raw image data (RGB components) ? Or is it a bmp (meaning with header etc... like a file) ?

  • Thank you for your response.

    As you can see in the last piece of code, it's just a data stream from a Bitmap.
    Actually, that's what I'm trying to do, reproducing that code as QT code.

  • Lifetime Qt Champion

    Since these are raw bytes, use the constructor of "QImage": to build it.

    Also you don't need to create a new QPixmap, use:

    Hope it helps

  • Moderators

    as SGaist already asked: what is the datatype of the stored binary in your database? The code doesn't clears it up.

    Is it really stored in the "BPM file format":

  • Thank you SGaist, but I have posted 2 ways of doing it, one with Pixmap, and anotherone without Pixmap.
    I use that only for buffering purposes, but it's not working anyway.

    raven-worx, as you can see in the last line of the old code:


    it's just a BMP in a Stream format, within a database.

  • Moderators

    Bitmap doesn't necessarily mean that's its really a BMP ... especially when i don't know the framework you are using.

    Seems the QByteArray valid?
    What happens when you write the QByteArray to a file and check if you can open?

  • I also tried to write that data in a file, but no success.
    What I realized debugging is that the "Loadfromdata" function is not working with the data I'm inserting.

    @ IMat->Picture->Bitmap->LoadFromStream(Str);@

    The IMat is a TImage from C++ Builder libraries.
    The memory Stream contains the Buffer from the query of the Database:

    @TByteDynArray Buffer=DataModuleCET->ADOQuery2->FieldByName("Data")->AsBytes;@

  • Lifetime Qt Champion

    Then are you sure your are not getting only the RGB data of the image ?

  • As it's not working, I'm not sure of anything J
    What would be the solution if it were the RGB data?

  • Lifetime Qt Champion

    If you have somewhere the image size (width, height), you can use QImage giving the proper image format and then make a QPixmap from it

  • Thank you for your help SGaist, I got stuck since I've discovered that the data storaged in that database is coded in a C++ Builder "whoknowshow" system.
    I quit and try to find an alternative way.

Log in to reply