Can't display QImage stored and read from sqlite database



  • Hi,

    I am doing kinda RnD here. I want to do the following in that sequence:

    • read an image and save its data to a sqlite database
    • now read the image from database
    • display the read data using pixmap to a QLabel

    I am using the following code for this:

    create database:

    @
    bool MainWindow::openDatabase()
    {
    QString databaseName("c:\data\metadata.sqlite");

    mDatabase = new QSqlDatabase();
    *mDatabase = QSqlDatabase::addDatabase("QSQLITE");
    
    mDatabase->setDatabaseName(databaseName);
    
    // Open databasee
    bool err = false;
    err = mDatabase->open();
    
    if (mDatabase->isOpen())
    {
        // create db table
        QSqlQuery query;
        QString sqlQuery = QString("CREATE TABLE IF NOT EXISTS thumbnails ( image BLOB );");
        err = query.exec(sqlQuery);
        query.clear();
    
        if (!err)
            return err;
    }
    return err;
    

    }
    @

    read image to db:

    @
    bool MainWindow::getThumbnailImage(QString msisdn, QByteArray outImageData)
    {
    QSqlQuery query(QString("SELECT image FROM thumbnails"));

    while (query.next())
    {
    query.next();
    outImageData = query.value(0).toByteArray();
    }
    query.clear();
    return true;
    

    }
    @

    write image from db:

    @
    bool MainWindow::addToThumbnailList(QString msisdn, QByteArray imageData)
    {
    QSqlQuery query;
    query.prepare("INSERT INTO thumbnails(image) VALUES(?)");
    query.addBindValue(imageData);
    bool ret = query.exec();
    return ret;
    }
    @

    display the image to QLabel

    @
    QLabel* label = new QLabel(this);
    QImage image;
    image.fromData(outData);

    label->setPixmap(QPixmap::fromImage(image).scaled(200, 200));
    label->setGeometry(30, 100, 200, 200);
    label->show();
    

    @

    Well the image is not getting displayed on the screen. Screen is blank. Any thoughts??

    Thanks


  • Lifetime Qt Champion

    Hi,

    Do you save only the image raw data ? In that case the new QImage won't know it's format size etc...



  • Ok thanks for reply.

    So what do you suggest?


  • Lifetime Qt Champion

    Save it in a known format like png, use QImage's save function to a QIODevice, for example a QBuffer, then put the content of the QBuffer's buffer in the database



  • Ok thanks.

    Just to make sure i am doing it right, i tried to save an image on newly created file say image.dat . I saved the bytearray to the file. Then tried to read the same data in qbytearray and passing this array to a QImage object which could be used by Qpixmap to display it onto the label. Something like this.
    Don't know what is wrong with this.

    @
    QByteArray inData;
    QByteArray outData;

    QFile readImage("/balloons.png");

    if(readImage.open(QIODevice::ReadOnly))
    {
    inData = readImage.readAll();
    }
    readImage.close();

    QFile* newImage = new QFile("/myImage.dat");

    if(newImage->open(QIODevice::WriteOnly)
    {
    newImage->write(inData);
    }
    newImage->close();

    QFile dispImage("/myImage.dat");

    if(dispImage.open(QIODevice::ReadOnly)
    {
    outData = dispImage.readAll();
    }
    dispImage.close();

    QImage image;
    image.fromData(outData); // does not work
    image.load("/balloons.png") // works perfectly
    QLabel* label = new QLabel(this);
    label->setPixmap(QPixmap::fromImage(image));
    label->show();
    @

    this code does not displays image on the label. any thoughts??


  • Lifetime Qt Champion

    You don't don't do anything if the various open fails, at least print an error message



  • yeah i know that...i just wrote the code directly here but do you see any issue with the code?

    I checked with the size of the read file and then compared it with the newly created file size. They are exactly the same. That means the data was written and read correctly.


  • Lifetime Qt Champion

    Writing in the root directory is rarely a good idea and not doing anything in case something can fail looks always as an issue to me :)

    What appends if you do this:

    @image.fromData(outData, "png");@

    ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.