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.
  • GREYONG GREYON

    Hello people, i need your help, i want to able to get image in computer file ,the insert in a database and late retrieve it from data and show it on the label. Currently am just able so load it and save it as file pathof is not very good idea ,I Want a user to be able to open a file dialogand select the file which can be converted to QByteArray and save in data base as BLOB and later retrieved and displayed on label.I think my major chellenge is on how to collect it from computer files by opening file dialogand save it as QByteArray. Your help will be greatly appreciated . My code below

    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);
    }
    
    
    void HOME::on_REGISTER_clicked()
    {
      /*  QSqlDatabase sqlitedabase=QSqlDatabase::addDatabase("QSQLITE");
        sqlitedabase.setDatabaseName("C:/DBsqlite/connect2.sqlite");
        sqlitedabase.open();*/
        QString username = ui->USERNAME_REG->text();
        QString email=ui->E_MAIL->text();
        QString password =ui->PASSWORD_REG->text();
        QString phone =ui->PHONE->text();
    
        if(username=="" || email==""|| password==""||phone=="")
        {
    
          QMessageBox::warning(this,"warning","PLEASE ENTER ALL THE REQUIRED DETAILS");
        }
        else
          {
            //checking to avoid duplication of data
          QSqlQuery query;
            query.prepare("SELECT EMAIL,PHONE FROM USERS WHERE EMAIL=:EMAIL AND PHONE=:PHONE");
    
               //query.bindValue(":USERNAME",username);
                query.bindValue(":EMAIL",email);
                query.bindValue(":PHONE",phone);
    
             if(query.exec()){
    
                    //QMessageBox::warning(this,"warninng","query successful!");
                     }
            if(query.next()){
    
                    QMessageBox::warning(this,"WARNING","CHECK YOUR E-MAIL OR PHONE NUMBER PLEASE\n"
                                                  "ONE OF THESE HAVE BEEN USED BY OTHER USERS ALREADY!");
    
            }
               // QSqlQuery qry;
            else
                  {
        //running insert query
    
        QSqlQuery qry;
    
        qry.prepare("INSERT INTO Users(USERNAME,EMAIL,PASSWORD,PHONE,IMAGEDATA)"
                    "VALUES(:USERNAME, :EMAIL, :PASSWORD, :PHONE,:IMAGEDATA)");
    
        qry.bindValue(":USERNAME",username);
        qry.bindValue(":EMAIL",email);
        qry.bindValue(":PASSWORD",password);
        qry.bindValue(":PHONE",phone);
        qry.bindValue(":IMAGEDATA",bigimage);
    
        if(qry.exec())
            {
           QMessageBox::information(this,"WELCOME","YOU HAVE SUCCESSFULLY REGISTERED!\nYOU CAN NOW LOGIN!");
        QSqlQuery qry;
         qry.prepare("SELECT IMAGEDATA FROM  Users WHERE USERNAME=:USERNAME AND PASSWORD=:PASSWORD");
         qry.bindValue(":USERNAME",username);
         qry.bindValue(":PASSWORD",password);
         qry.exec();
         qry.lastError();
          while(qry.next())
            {
         QString outimage =qry.value(0).toString();
         QPixmap outpixmap=QPixmap(outimage);
        // outpixmap.loadFromData(outimage);
        int h=ui->label_6->height();
        int d=ui->label_6->width();
        outpixmap.scaled(100,80,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
        ui->label_6->setPixmap(outpixmap);
          }
         
        }
        else
       {
    
           QMessageBox::warning(this,"WARNING!","PLEASE TURN ON THE SYSTEM FIRST!");
    
    }
    
    }
    
    }
    ui->E_MAIL->clear();
    ui->PASSWORD_REG->clear();
    ui->PHONE->clear();
    ui->USERNAME_REG->clear();
    
    }
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

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

    Your help will be greatly appreciated

    What exact help do you need? What does not work? If you want to ask user to select a location and file name to store the image then use https://doc.qt.io/qt-5/qfiledialog.html#getSaveFileName. To store data into that file use QFile.

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

    1 Reply Last reply
    3
    • GREYONG GREYON

      Hello people, i need your help, i want to able to get image in computer file ,the insert in a database and late retrieve it from data and show it on the label. Currently am just able so load it and save it as file pathof is not very good idea ,I Want a user to be able to open a file dialogand select the file which can be converted to QByteArray and save in data base as BLOB and later retrieved and displayed on label.I think my major chellenge is on how to collect it from computer files by opening file dialogand save it as QByteArray. Your help will be greatly appreciated . My code below

      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);
      }
      
      
      void HOME::on_REGISTER_clicked()
      {
        /*  QSqlDatabase sqlitedabase=QSqlDatabase::addDatabase("QSQLITE");
          sqlitedabase.setDatabaseName("C:/DBsqlite/connect2.sqlite");
          sqlitedabase.open();*/
          QString username = ui->USERNAME_REG->text();
          QString email=ui->E_MAIL->text();
          QString password =ui->PASSWORD_REG->text();
          QString phone =ui->PHONE->text();
      
          if(username=="" || email==""|| password==""||phone=="")
          {
      
            QMessageBox::warning(this,"warning","PLEASE ENTER ALL THE REQUIRED DETAILS");
          }
          else
            {
              //checking to avoid duplication of data
            QSqlQuery query;
              query.prepare("SELECT EMAIL,PHONE FROM USERS WHERE EMAIL=:EMAIL AND PHONE=:PHONE");
      
                 //query.bindValue(":USERNAME",username);
                  query.bindValue(":EMAIL",email);
                  query.bindValue(":PHONE",phone);
      
               if(query.exec()){
      
                      //QMessageBox::warning(this,"warninng","query successful!");
                       }
              if(query.next()){
      
                      QMessageBox::warning(this,"WARNING","CHECK YOUR E-MAIL OR PHONE NUMBER PLEASE\n"
                                                    "ONE OF THESE HAVE BEEN USED BY OTHER USERS ALREADY!");
      
              }
                 // QSqlQuery qry;
              else
                    {
          //running insert query
      
          QSqlQuery qry;
      
          qry.prepare("INSERT INTO Users(USERNAME,EMAIL,PASSWORD,PHONE,IMAGEDATA)"
                      "VALUES(:USERNAME, :EMAIL, :PASSWORD, :PHONE,:IMAGEDATA)");
      
          qry.bindValue(":USERNAME",username);
          qry.bindValue(":EMAIL",email);
          qry.bindValue(":PASSWORD",password);
          qry.bindValue(":PHONE",phone);
          qry.bindValue(":IMAGEDATA",bigimage);
      
          if(qry.exec())
              {
             QMessageBox::information(this,"WELCOME","YOU HAVE SUCCESSFULLY REGISTERED!\nYOU CAN NOW LOGIN!");
          QSqlQuery qry;
           qry.prepare("SELECT IMAGEDATA FROM  Users WHERE USERNAME=:USERNAME AND PASSWORD=:PASSWORD");
           qry.bindValue(":USERNAME",username);
           qry.bindValue(":PASSWORD",password);
           qry.exec();
           qry.lastError();
            while(qry.next())
              {
           QString outimage =qry.value(0).toString();
           QPixmap outpixmap=QPixmap(outimage);
          // outpixmap.loadFromData(outimage);
          int h=ui->label_6->height();
          int d=ui->label_6->width();
          outpixmap.scaled(100,80,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
          ui->label_6->setPixmap(outpixmap);
            }
           
          }
          else
         {
      
             QMessageBox::warning(this,"WARNING!","PLEASE TURN ON THE SYSTEM FIRST!");
      
      }
      
      }
      
      }
      ui->E_MAIL->clear();
      ui->PASSWORD_REG->clear();
      ui->PHONE->clear();
      ui->USERNAME_REG->clear();
      
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #3

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

      collect it from computer files by opening file dialogand save it as QByteArray.

      Use QFileDialog::getOpenFileName() to prompt the user to pick an existing file. Open it with QFile and use its methods to read its content as a QByteArray. Send that to your database to store as a BLOB.

      GREYONG 1 Reply Last reply
      2
      • JonBJ JonB

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

        collect it from computer files by opening file dialogand save it as QByteArray.

        Use QFileDialog::getOpenFileName() to prompt the user to pick an existing file. Open it with QFile and use its methods to read its content as a QByteArray. Send that to your database to store as a BLOB.

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

        @JonB thanks for you help,however since am kind of stack,do you have any written example on how to do that?

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

          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 1 Reply Last reply
          0
          • 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 Offline
              JonBJ Offline
              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 Offline
                            JonBJ Offline
                            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 Offline
                                    JonBJ Offline
                                    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

                                          • Login

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