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. Save and Load pictures in QT c++ from SQLite
QtWS25 Last Chance

Save and Load pictures in QT c++ from SQLite

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 2.4k Views
  • 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.
  • S Offline
    S Offline
    Sapok_5
    wrote on 8 Apr 2022, 15:20 last edited by
    #1

    i'm new to whole QT and Sqlite. but i somehow managed to learn both and work. now, i've come to face new problem, i.e, save and load picture in QT using sqlite.
    can someone help???

    so the idea is to, ask user to choose a picture from pc and then save it in database, then in new window, load that picture using same database table.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 8 Apr 2022, 15:56 last edited by VRonin 4 Aug 2022, 15:56
      #2

      Disclaimer: it's ususally best to save the image in a folder and save the path in the DB rather than storing the data directly in the database


      You can use the BLOB type of SQLite then you can store the image in a QByteArray like:

      QByteArray imageData;
      QDataStream imageStream(&imageData,QIODevice::WriteOnly);
      imageStream << image;
      

      then you can use imageData in addBindValue/bindValue for your QSqlQuery.

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      S 1 Reply Last reply 8 Apr 2022, 16:39
      2
      • A Offline
        A Offline
        A.A.SEZEN
        wrote on 8 Apr 2022, 16:01 last edited by
        #3

        https://wiki.qt.io/How_to_Store_and_Retrieve_Image_on_SQLite

        S 1 Reply Last reply 9 Apr 2022, 16:42
        3
        • V VRonin
          8 Apr 2022, 15:56

          Disclaimer: it's ususally best to save the image in a folder and save the path in the DB rather than storing the data directly in the database


          You can use the BLOB type of SQLite then you can store the image in a QByteArray like:

          QByteArray imageData;
          QDataStream imageStream(&imageData,QIODevice::WriteOnly);
          imageStream << image;
          

          then you can use imageData in addBindValue/bindValue for your QSqlQuery.

          S Offline
          S Offline
          Sapok_5
          wrote on 8 Apr 2022, 16:39 last edited by
          #4

          @VRonin in
          imageStream << image;
          image stands for??

          V 1 Reply Last reply 8 Apr 2022, 17:31
          0
          • S Sapok_5
            8 Apr 2022, 16:39

            @VRonin in
            imageStream << image;
            image stands for??

            V Offline
            V Offline
            VRonin
            wrote on 8 Apr 2022, 17:31 last edited by
            #5

            @Sapok_5 said in Save and Load pictures in QT c++ from SQLite:

            image stands for??

            Your image. a QImage or QPixmap. The wiki link above is a great resource

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • A A.A.SEZEN
              8 Apr 2022, 16:01

              https://wiki.qt.io/How_to_Store_and_Retrieve_Image_on_SQLite

              S Offline
              S Offline
              Sapok_5
              wrote on 9 Apr 2022, 16:42 last edited by
              #6

              @A-A-SEZEN

              QString filename2 = QFileDialog::getOpenFileName(this, tr("Open Image"), "/", tr("Image Files (*.png *.jpg *.bmp)"));
              QPixmap pixmapTarget = QPixmap(filename2);
              pixmapTarget = pixmapTarget.scaled(ui->candidate_2_img->width(), ui->candidate_2_img->height(), Qt::IgnoreAspectRatio , Qt::SmoothTransformation);
              ui->candidate_2_img->setPixmap(pixmapTarget);
              

              i did this to get and display the output picture. but now i can't figure out how to save it.
              i tried saving Qpixmap but then, in database, it shows, blank..
              then, i tried savving path, and now, im stuck with how to use path to show image again??
              while that wiki was great, i still can't figure out how to use it.

              J A 2 Replies Last reply 9 Apr 2022, 18:34
              0
              • S Sapok_5
                9 Apr 2022, 16:42

                @A-A-SEZEN

                QString filename2 = QFileDialog::getOpenFileName(this, tr("Open Image"), "/", tr("Image Files (*.png *.jpg *.bmp)"));
                QPixmap pixmapTarget = QPixmap(filename2);
                pixmapTarget = pixmapTarget.scaled(ui->candidate_2_img->width(), ui->candidate_2_img->height(), Qt::IgnoreAspectRatio , Qt::SmoothTransformation);
                ui->candidate_2_img->setPixmap(pixmapTarget);
                

                i did this to get and display the output picture. but now i can't figure out how to save it.
                i tried saving Qpixmap but then, in database, it shows, blank..
                then, i tried savving path, and now, im stuck with how to use path to show image again??
                while that wiki was great, i still can't figure out how to use it.

                J Offline
                J Offline
                JonB
                wrote on 9 Apr 2022, 18:34 last edited by
                #7

                @Sapok_5
                The example code at https://wiki.qt.io/How_to_Store_and_Retrieve_Image_on_SQLite seems to show how to save to SQL.

                1 Reply Last reply
                1
                • S Sapok_5
                  9 Apr 2022, 16:42

                  @A-A-SEZEN

                  QString filename2 = QFileDialog::getOpenFileName(this, tr("Open Image"), "/", tr("Image Files (*.png *.jpg *.bmp)"));
                  QPixmap pixmapTarget = QPixmap(filename2);
                  pixmapTarget = pixmapTarget.scaled(ui->candidate_2_img->width(), ui->candidate_2_img->height(), Qt::IgnoreAspectRatio , Qt::SmoothTransformation);
                  ui->candidate_2_img->setPixmap(pixmapTarget);
                  

                  i did this to get and display the output picture. but now i can't figure out how to save it.
                  i tried saving Qpixmap but then, in database, it shows, blank..
                  then, i tried savving path, and now, im stuck with how to use path to show image again??
                  while that wiki was great, i still can't figure out how to use it.

                  A Offline
                  A Offline
                  A.A.SEZEN
                  wrote on 10 Apr 2022, 08:50 last edited by A.A.SEZEN 4 Oct 2022, 08:51
                  #8

                  @Sapok_5

                          QByteArray baImage;
                          QBuffer buffer(&baImage);
                          buffer.open(QIODevice::WriteOnly);
                          QPixmap pixmap(ui->candidate_2_img->grab()); // candidate_2_img as QLabel
                          pixmap.save(&buffer, "jpg", 60);
                          //db save process .. INSERT OR IGNORE INTO ...
                          query.bindValue(":BlobValue", baImage);
                          query.exec();
                  
                  
                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Sapok_5
                    wrote on 11 Apr 2022, 11:40 last edited by VRonin 4 Nov 2022, 11:51
                    #9

                    So i tried different approach here.
                    instead of putting picture inside, i insert its path. i.e,

                     QString filename = QFileDialog::getOpenFileName(this, tr("Open Image"), "/", tr("Image Files (*.png *.jpg *.bmp)"));
                        QPixmap pixmapTarget = QPixmap(filename);
                        pixmapTarget = pixmapTarget.scaled(ui->candidate_1_img->width(), ui->candidate_1_img->height(), Qt::IgnoreAspectRatio , Qt::SmoothTransformation);
                        ui->candidate_1_img->setPixmap(pixmapTarget);
                        QSqlQuery query;
                        query.prepare( "INSERT INTO candidate (candidate_1_img) VALUES (?)" );
                                                  query.addBindValue(filename);
                                                  if(query.exec()){
                                                      ui->connect->setText("Upload Success, Candidate 1 Picture Uploaded");
                    
                                                  }
                                                  else{
                                                      ui->connect->setText("Upload failed, Please try again");
                    }
                    

                    now, in different window:, if i want the picture to be shown,
                    i do this:

                      QString candidate_1_img;
                    QString p1;
                            QSqlQuery query("SELECT candidate_1_img FROM candidate");
                            while (query.next()) {
                              p1 =   candidate_1_img.append( query.value(0).toString());
                    
                            }
                            QString filename = p1;
                            QPixmap pixmapTarget = QPixmap(filename);
                            pixmapTarget = pixmapTarget.scaled(ui->candidate_1_img->width(), ui->candidate_1_img->height(), Qt::IgnoreAspectRatio , Qt::SmoothTransformation);
                            ui->candidate_1_img->setPixmap(pixmapTarget);
                    
                    J 1 Reply Last reply 11 Apr 2022, 12:42
                    0
                    • S Sapok_5
                      11 Apr 2022, 11:40

                      So i tried different approach here.
                      instead of putting picture inside, i insert its path. i.e,

                       QString filename = QFileDialog::getOpenFileName(this, tr("Open Image"), "/", tr("Image Files (*.png *.jpg *.bmp)"));
                          QPixmap pixmapTarget = QPixmap(filename);
                          pixmapTarget = pixmapTarget.scaled(ui->candidate_1_img->width(), ui->candidate_1_img->height(), Qt::IgnoreAspectRatio , Qt::SmoothTransformation);
                          ui->candidate_1_img->setPixmap(pixmapTarget);
                          QSqlQuery query;
                          query.prepare( "INSERT INTO candidate (candidate_1_img) VALUES (?)" );
                                                    query.addBindValue(filename);
                                                    if(query.exec()){
                                                        ui->connect->setText("Upload Success, Candidate 1 Picture Uploaded");
                      
                                                    }
                                                    else{
                                                        ui->connect->setText("Upload failed, Please try again");
                      }
                      

                      now, in different window:, if i want the picture to be shown,
                      i do this:

                        QString candidate_1_img;
                      QString p1;
                              QSqlQuery query("SELECT candidate_1_img FROM candidate");
                              while (query.next()) {
                                p1 =   candidate_1_img.append( query.value(0).toString());
                      
                              }
                              QString filename = p1;
                              QPixmap pixmapTarget = QPixmap(filename);
                              pixmapTarget = pixmapTarget.scaled(ui->candidate_1_img->width(), ui->candidate_1_img->height(), Qt::IgnoreAspectRatio , Qt::SmoothTransformation);
                              ui->candidate_1_img->setPixmap(pixmapTarget);
                      
                      J Offline
                      J Offline
                      JonB
                      wrote on 11 Apr 2022, 12:42 last edited by
                      #10

                      @Sapok_5 said in Save and Load pictures in QT c++ from SQLite:

                      instead of putting picture inside, i insert its path. i.e,

                      Well, that's a totally different approach. Whether that is valid depends on how you expect your program to be run. By saving the filename instead of the pixmap content your code relies on the same file being in the same place on the external disc whenever it is wanted by your app. E.g. won't work for another user, or if you change your disk content. I would have expected you to want to save the pixmap into the database, but only you know whether you are wanting to store just a filename for later retrieval or the actual picture itself.

                      while (query.next()) {

                      This will be returning false and won't enter the loop since you have not executed the query. Not to mention, that will lead your code to loading an empty filename and returning an empty QPixmap.

                      1 Reply Last reply
                      2

                      3/10

                      8 Apr 2022, 16:01

                      topic:navigator.unread, 7
                      • Login

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