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 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 Offline
      Christian EhrlicherC Offline
      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 Offline
            JonBJ Offline
            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
            • 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();

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

              This is the correct one

              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();
              
              
              
              1 Reply Last reply
              0
              • HoMaH HoMa

                @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 Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #27

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

                QPixmap pixmap("../sqlPixmap/image.jpg");
                db.setDatabaseName ("../sqlPixmap/db.sqlite");

                As @Christian-Ehrlicher commented, you simply should not use a relative path here, as you just do not know where that will be. See my https://forum.qt.io/topic/138832/export-qtableview-to-pdf-with-a-push-button/8 to another user on the same matter.

                HoMaH 1 Reply Last reply
                0
                • JonBJ JonB

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

                  QPixmap pixmap("../sqlPixmap/image.jpg");
                  db.setDatabaseName ("../sqlPixmap/db.sqlite");

                  As @Christian-Ehrlicher commented, you simply should not use a relative path here, as you just do not know where that will be. See my https://forum.qt.io/topic/138832/export-qtableview-to-pdf-with-a-push-button/8 to another user on the same matter.

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

                  @JonB Thx for your efforts - I appreciate that. But this is a test program for the single purpose of playing around with sqlite, blobs (and meanwhile "in memory"). Anyone who wants to use the code will have to modify the path to the picture - no matter if it is absolute or relative. For this reason I think it makes no difference in this specific case.

                  1 Reply Last reply
                  1
                  • HoMaH HoMa

                    @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;
                        }
                    
                    
                    GREYONG Offline
                    GREYONG Offline
                    GREYON
                    wrote on last edited by
                    #29

                    @HoMa thanks for that code I will try it as well

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

                      Couldn't resist and created a version, which works also with an in memory database.
                      @GREYON : Pls check out https://github.com/Schachigel/Qt-QPixmap-2-SQLite.git

                      have fun!

                      GREYONG 1 Reply Last reply
                      1
                      • HoMaH HoMa

                        Couldn't resist and created a version, which works also with an in memory database.
                        @GREYON : Pls check out https://github.com/Schachigel/Qt-QPixmap-2-SQLite.git

                        have fun!

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

                        @HoMa okay thanks let me check bro

                        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