Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Can't display QImage stored and read from sqlite database
Forum Updated to NodeBB v4.3 + New Features

Can't display QImage stored and read from sqlite database

Scheduled Pinned Locked Moved Mobile and Embedded
8 Posts 2 Posters 5.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    raj.qtdev
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        raj.qtdev
        wrote on last edited by
        #3

        Ok thanks for reply.

        So what do you suggest?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            raj.qtdev
            wrote on last edited by
            #5

            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??

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • R Offline
                R Offline
                raj.qtdev
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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");@

                  ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved