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
Forum Updated to NodeBB v4.3 + New Features

Save and Load pictures in QT c++ from SQLite

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 2.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.
  • S Offline
    S Offline
    Sapok_5
    wrote on 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
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #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
      2
      • A.A.SEZENA Offline
        A.A.SEZENA Offline
        A.A.SEZEN
        wrote on last edited by
        #3

        https://wiki.qt.io/How_to_Store_and_Retrieve_Image_on_SQLite

        S 1 Reply Last reply
        3
        • VRoninV VRonin

          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 last edited by
          #4

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

          VRoninV 1 Reply Last reply
          0
          • S Sapok_5

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

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on 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.SEZENA A.A.SEZEN

              https://wiki.qt.io/How_to_Store_and_Retrieve_Image_on_SQLite

              S Offline
              S Offline
              Sapok_5
              wrote on 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.

              JonBJ A.A.SEZENA 2 Replies Last reply
              0
              • S Sapok_5

                @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.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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

                  @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.A.SEZENA Offline
                  A.A.SEZENA Offline
                  A.A.SEZEN
                  wrote on last edited by A.A.SEZEN
                  #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 last edited by VRonin
                    #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);
                    
                    JonBJ 1 Reply Last reply
                    0
                    • S Sapok_5

                      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);
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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

                      • Login

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