Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Loading image from computer files save in database and display later
Forum Updated to NodeBB v4.3 + New Features

Loading image from computer files save in database and display later

Scheduled Pinned Locked Moved Solved General and Desktop
31 Posts 8 Posters 3.3k Views 3 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.
  • HoMaH HoMa

    You stuff a QByteArray into the database, but you fetch a QString? This does not look good to me. I guess you should try to convert to a QByte Array here:

    QString outimage =qry.value(0).toString();
    QPixmap outpixmap=QPixmap(outimage);
    
    GREYONG Offline
    GREYONG Offline
    GREYON
    wrote on last edited by
    #6

    @HoMa thanks very much for your observation , however that was just the last option after fetching QByteArray like this

    QByteArray outimage =qry.value(0).toByteArray();
    QPixmap outpixmap=QPixmap();
    outpixmap.loadFromData(outpixmap);
    ui->label_6->setPixmap(outpixmap);
    ```  could not display anything on the label.At least when I used QString I was able to displayt the image though it's not the correct way of doing it.
    JonBJ 1 Reply Last reply
    0
    • GREYONG GREYON

      @HoMa thanks very much for your observation , however that was just the last option after fetching QByteArray like this

      QByteArray outimage =qry.value(0).toByteArray();
      QPixmap outpixmap=QPixmap();
      outpixmap.loadFromData(outpixmap);
      ui->label_6->setPixmap(outpixmap);
      ```  could not display anything on the label.At least when I used QString I was able to displayt the image though it's not the correct way of doing it.
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #7

      @GREYON said in Loading image from computer files save in database and display later:

      outpixmap.loadFromData(outpixmap);

      That's an interesting line that you have copied from your source code.

      GREYONG 1 Reply Last reply
      1
      • JonBJ JonB

        @GREYON said in Loading image from computer files save in database and display later:

        outpixmap.loadFromData(outpixmap);

        That's an interesting line that you have copied from your source code.

        GREYONG Offline
        GREYONG Offline
        GREYON
        wrote on last edited by
        #8

        @JonB why have you said that its an interesting line?

        jsulmJ 1 Reply Last reply
        0
        • GREYONG GREYON

          @JonB why have you said that its an interesting line?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #9

          @GREYON said in Loading image from computer files save in database and display later:

          why have you said that its an interesting line?

          Take a closer look at that line please.
          You're trying to load data into outpixmap from outpixmap - does that make sense? Shouldn't you load the pixmap from outimage?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          GREYONG 1 Reply Last reply
          0
          • jsulmJ jsulm

            @GREYON said in Loading image from computer files save in database and display later:

            why have you said that its an interesting line?

            Take a closer look at that line please.
            You're trying to load data into outpixmap from outpixmap - does that make sense? Shouldn't you load the pixmap from outimage?

            GREYONG Offline
            GREYONG Offline
            GREYON
            wrote on last edited by
            #10

            @jsulm okay let me look at it sir

            GREYONG 1 Reply Last reply
            0
            • GREYONG GREYON

              @jsulm okay let me look at it sir

              GREYONG Offline
              GREYONG Offline
              GREYON
              wrote on last edited by
              #11

              @GREYON You are right it supposed to be

              outpixmap.loadDataFrom(outimage)
              

              It was just an error when I was typing to post here but that's how it was in the actual cade.

              jsulmJ JonBJ 2 Replies Last reply
              0
              • GREYONG GREYON

                @GREYON You are right it supposed to be

                outpixmap.loadDataFrom(outimage)
                

                It was just an error when I was typing to post here but that's how it was in the actual cade.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #12

                @GREYON What does outpixmap.loadFromData(outpixmap) return?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #13

                  how sure are you, that IMAGEDATA is actually of type BLOB ?


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  0
                  • GREYONG GREYON

                    @GREYON You are right it supposed to be

                    outpixmap.loadDataFrom(outimage)
                    

                    It was just an error when I was typing to post here but that's how it was in the actual cade.

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by JonB
                    #14

                    @GREYON said in Loading image from computer files save in database and display later:

                    It was just an error when I was typing to post here but that's how it was in the actual cade.

                    Please copy and paste source code, how else can people know what you actually have if you type it in? Wasted too much time over the years with people saying something is in their source when it turns out it is not....

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #15

                      Hi
                      Did you look over this example?
                      https://wiki.qt.io/How_to_Store_and_Retrieve_Image_on_SQLite

                      GREYONG 1 Reply Last reply
                      0
                      • GREYONG Offline
                        GREYONG Offline
                        GREYON
                        wrote on last edited by
                        #16

                        When i use this code :

                          qry.lastError();
                              while(qry.next())
                                {
                             QByteArray outimage =qry.value(0).toByteArray();
                             QPixmap outpixmap=QPixmap();
                            // outpixmap.loadFromData(outimage);
                            int h=ui->label_6->height();
                            int d=ui->label_6->width();
                            outpixmap.loadFromData(outimage);
                            outpixmap.scaled(d,h,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
                            ui->label_6->setPixmap(outpixmap);
                              }
                             
                            }
                        

                        nothing is being displayed on the label ,am just getting the following error and i don't understand why .
                        ![alt text](b590ce5b-224d-4ce6-bc90-6896cd4b0685-Capturecc.PNG

                        1 Reply Last reply
                        0
                        • mrjjM mrjj

                          Hi
                          Did you look over this example?
                          https://wiki.qt.io/How_to_Store_and_Retrieve_Image_on_SQLite

                          GREYONG Offline
                          GREYONG Offline
                          GREYON
                          wrote on last edited by
                          #17

                          @mrjj thanks yes did look over it only that it was not very clear to me because it talk much using the screen short unlike when you loading it from the computer.I think its just because am still a learner in qt.

                          JonBJ 1 Reply Last reply
                          0
                          • GREYONG GREYON

                            @mrjj thanks yes did look over it only that it was not very clear to me because it talk much using the screen short unlike when you loading it from the computer.I think its just because am still a learner in qt.

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by
                            #18

                            @GREYON
                            Start by checking the length of qry.value(0).toByteArray(), and indeed the length/number of bytes at the SQL side. Is this perhaps 0? loadFromData() returns a bool for a reason --- you need to check it.

                            1 Reply Last reply
                            0
                            • HoMaH Offline
                              HoMaH Offline
                              HoMa
                              wrote on last edited by HoMa
                              #19

                              @GREYON
                              I was curiouse and typed in the code below. It worked w/o problems. So you are on the right track - maybe you go line to line and compare my code with yours.
                              btw: your

                              QPixmap out =QPixmap();
                              

                              should be

                              QPixmap out;
                              

                              and

                              QPixmap outpixmap=QPixmap(outimage)
                              

                              should be

                              QPixmap outpixmap(outimage);
                              

                              This is not the problem you have, just inefficient code.

                              To the experts here: I use the code below and only switch to a sqlite in memory database it no longer works. Any idea why this is?

                              void MainWindow::on_pushButton_clicked()
                              {
                                  QPixmap pixmap("../sqlPixmap/image.jpg");
                                  QByteArray ba;
                                  QBuffer bu(&ba);
                                  bu.open(QIODevice::WriteOnly);
                                  pixmap.save(&bu);
                              
                                  QSqlDatabase db =QSqlDatabase::addDatabase ("QSQLITE");
                                  db.setDatabaseName ("../sqlPixmap/db.sqlite");
                              //    db.setDatabaseName (":memory:");
                                  db.open();
                              
                                  QSqlQuery v("SELECT sqlite_version()");
                                  v.next();
                                  qDebug() << "sqlite version " << v.value (0);
                              
                                  QSqlQuery q ("CREATE TABLE t (c)");
                              
                                  q.prepare ("INSERT INTO t (c) VALUES (:pic)");
                                  q.bindValue (":pic", ba);
                                  if( ! q.exec()) {
                                      qDebug() << "could not store data";
                                  }
                              
                                  /////////////////////////////////////////////
                              
                                  q.prepare ("SELECT c FROM t");
                                  if( ! q.exec()) {
                                      qDebug() << "could in select query";
                                  }
                                  if( ! q.next()) {
                                      qDebug() << "failed to retrieve data";
                                  }
                                  QByteArray readBa =q.value (0).toByteArray ();
                              
                                  QPixmap readPic;
                                  readPic.loadFromData(readBa);
                                  ui->lbl->setPixmap (readPic);
                                  ui->lbl->setScaledContents (true);
                              }
                              
                              Christian EhrlicherC 1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #20

                                Hi,

                                What exactly does not work ?

                                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
                                • HoMaH Offline
                                  HoMaH Offline
                                  HoMa
                                  wrote on last edited by
                                  #21

                                  Sorry for missing out on that.
                                  All SQL calls have positive result, but the array is empty. Strange …

                                  1 Reply Last reply
                                  0
                                  • HoMaH HoMa

                                    @GREYON
                                    I was curiouse and typed in the code below. It worked w/o problems. So you are on the right track - maybe you go line to line and compare my code with yours.
                                    btw: your

                                    QPixmap out =QPixmap();
                                    

                                    should be

                                    QPixmap out;
                                    

                                    and

                                    QPixmap outpixmap=QPixmap(outimage)
                                    

                                    should be

                                    QPixmap outpixmap(outimage);
                                    

                                    This is not the problem you have, just inefficient code.

                                    To the experts here: I use the code below and only switch to a sqlite in memory database it no longer works. Any idea why this is?

                                    void MainWindow::on_pushButton_clicked()
                                    {
                                        QPixmap pixmap("../sqlPixmap/image.jpg");
                                        QByteArray ba;
                                        QBuffer bu(&ba);
                                        bu.open(QIODevice::WriteOnly);
                                        pixmap.save(&bu);
                                    
                                        QSqlDatabase db =QSqlDatabase::addDatabase ("QSQLITE");
                                        db.setDatabaseName ("../sqlPixmap/db.sqlite");
                                    //    db.setDatabaseName (":memory:");
                                        db.open();
                                    
                                        QSqlQuery v("SELECT sqlite_version()");
                                        v.next();
                                        qDebug() << "sqlite version " << v.value (0);
                                    
                                        QSqlQuery q ("CREATE TABLE t (c)");
                                    
                                        q.prepare ("INSERT INTO t (c) VALUES (:pic)");
                                        q.bindValue (":pic", ba);
                                        if( ! q.exec()) {
                                            qDebug() << "could not store data";
                                        }
                                    
                                        /////////////////////////////////////////////
                                    
                                        q.prepare ("SELECT c FROM t");
                                        if( ! q.exec()) {
                                            qDebug() << "could in select query";
                                        }
                                        if( ! q.next()) {
                                            qDebug() << "failed to retrieve data";
                                        }
                                        QByteArray readBa =q.value (0).toByteArray ();
                                    
                                        QPixmap readPic;
                                        readPic.loadFromData(readBa);
                                        ui->lbl->setPixmap (readPic);
                                        ui->lbl->setScaledContents (true);
                                    }
                                    
                                    Christian EhrlicherC Online
                                    Christian EhrlicherC Online
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #22

                                    @HoMa said in Loading image from computer files save in database and display later:

                                    QPixmap pixmap("../sqlPixmap/image.jpg");
                                    QByteArray ba;
                                    QBuffer bu(&ba);
                                    bu.open(QIODevice::WriteOnly);
                                    pixmap.save(&bu);
                                    

                                    Please check if the first line really works. Relaitve paths are not good since you don't know the current working directory of your program.

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    HoMaH 1 Reply Last reply
                                    2
                                    • GREYONG Offline
                                      GREYONG Offline
                                      GREYON
                                      wrote on last edited by
                                      #23

                                      @JonB thanks people for your help,after going through documentation and one of example which I was given a here ,I found out that the code below is one having a problem,it was storing a file in a data base as a file path and not as Byte.

                                      
                                      QByteArray HOME::bigimage;
                                      void HOME::on_commandLinkButton_clicked()
                                      {
                                       // QByteArray bigimage;
                                       QString filename=QFileDialog::getOpenFileName(this,tr("Open image"),"/",tr("Image Files(*.png;*.jpg;*.bmp)"));
                                       bigimage +=filename;
                                      
                                       QPixmap pixmap=QPixmap(filename);
                                       ui->label_8->setText(filename);
                                      }```
                                      
                                      So I had to change to something like this and it worked.
                                      
                                      

                                      QByteArray HOME::bigimage;
                                      void HOME::on_commandLinkButton_clicked()
                                      {
                                      // QByteArray bigimage;
                                      QString filename=QFileDialog::getOpenFileName(this,tr("Open image"),"/",tr("Image Files(.png;.jpg;*.bmp)"));

                                      Qfile file(filename)
                                      if(!file.open(QIODevice::ReadOnly) return;
                                      bigimage=file.readAll();

                                      JonBJ GREYONG 2 Replies Last reply
                                      0
                                      • Christian EhrlicherC Christian Ehrlicher

                                        @HoMa said in Loading image from computer files save in database and display later:

                                        QPixmap pixmap("../sqlPixmap/image.jpg");
                                        QByteArray ba;
                                        QBuffer bu(&ba);
                                        bu.open(QIODevice::WriteOnly);
                                        pixmap.save(&bu);
                                        

                                        Please check if the first line really works. Relaitve paths are not good since you don't know the current working directory of your program.

                                        HoMaH Offline
                                        HoMaH Offline
                                        HoMa
                                        wrote on last edited by
                                        #24

                                        @Christian-Ehrlicher It really does. I change only the file name from what is here to ":memory:" (like in the line below, which is commented out) and the picture can no longer be retreived.

                                        Here the code with all calls tested:

                                            try
                                            {
                                                QPixmap pixmap("../sqlPixmap/image.jpg");
                                                QByteArray ba;
                                                QBuffer bu(&ba);
                                                bu.open(QIODevice::WriteOnly);
                                                pixmap.save(&bu);
                                                if( pixmap.width ()<= 0 || pixmap.height () <= 0) throw "Loading image from file failed";
                                        
                                                QSqlDatabase db =QSqlDatabase::addDatabase ("QSQLITE");
                                                db.setDatabaseName ("../sqlPixmap/db.sqlite");
                                                //db.setDatabaseName (":memory:");
                                                if( ! db.open()) throw "database could not be opened";
                                        
                                                QSqlQuery v("SELECT sqlite_version()");
                                                Q_ASSERT(v.next());
                                                qDebug() << "sqlite version " << v.value (0).toString();
                                        
                                                QSqlQuery q ("CREATE TABLE t (c)"); // executes on creation
                                        
                                                q.prepare ("INSERT INTO t (c) VALUES (:pic)");
                                                q.bindValue (":pic", ba);
                                                if( ! q.exec()) throw "could not store data";
                                        
                                                /////////////////////////////////////////////
                                        
                                                q.prepare ("SELECT c FROM t");
                                                if( ! q.exec()) throw "failed in select query";
                                        
                                                if( ! q.next()) throw "failed to move to data set";
                                        
                                                QByteArray readBa =q.value (0).toByteArray ();
                                                if( 0 >= readBa.size ()) throw "returned data is empty";
                                        
                                                QPixmap readPic;
                                                readPic.loadFromData(readBa);
                                                ui->lbl->setPixmap (readPic);
                                                ui->lbl->setScaledContents (true);
                                            }
                                            catch(const char* error)
                                            {
                                                qDebug() << error;
                                                return;
                                            }
                                        
                                        
                                        JonBJ GREYONG 2 Replies Last reply
                                        0
                                        • GREYONG GREYON

                                          @JonB thanks people for your help,after going through documentation and one of example which I was given a here ,I found out that the code below is one having a problem,it was storing a file in a data base as a file path and not as Byte.

                                          
                                          QByteArray HOME::bigimage;
                                          void HOME::on_commandLinkButton_clicked()
                                          {
                                           // QByteArray bigimage;
                                           QString filename=QFileDialog::getOpenFileName(this,tr("Open image"),"/",tr("Image Files(*.png;*.jpg;*.bmp)"));
                                           bigimage +=filename;
                                          
                                           QPixmap pixmap=QPixmap(filename);
                                           ui->label_8->setText(filename);
                                          }```
                                          
                                          So I had to change to something like this and it worked.
                                          
                                          

                                          QByteArray HOME::bigimage;
                                          void HOME::on_commandLinkButton_clicked()
                                          {
                                          // QByteArray bigimage;
                                          QString filename=QFileDialog::getOpenFileName(this,tr("Open image"),"/",tr("Image Files(.png;.jpg;*.bmp)"));

                                          Qfile file(filename)
                                          if(!file.open(QIODevice::ReadOnly) return;
                                          bigimage=file.readAll();

                                          JonBJ Online
                                          JonBJ Online
                                          JonB
                                          wrote on last edited by
                                          #25

                                          @GREYON
                                          Not sure what happened to your QPixmap pixmap=QPixmap(filename); line now. After reading a QPixmap from a file (or elsewhere) you can/should always check bool QPixmap::isNull() const. That would cover e.g. "file not found" as well as "error reading contents of file as pixmap".

                                          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