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. Displaying more than one QImage from SQLite database.
QtWS25 Last Chance

Displaying more than one QImage from SQLite database.

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.9k 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.
  • deleted511D Offline
    deleted511D Offline
    deleted511
    wrote on last edited by deleted511
    #1

    For my project, I have compiled a live stream camera that takes pictures and stores the images as QImages and saves them into the database as 'PNG'. The problem I am having is displaying more than one QImage acquired from my database on different labels. What I am trying to do is have a widget that shows the livestream and three labels beside the widget to display the last three images taken. Although I get the first label to show the last image taken from the live stream, i want label 2 to inherit label 1s picture and label 3 to inherit label 2s picture. My database is working accordingly because every time I press 'picture' it saves into the database. Here is my code:

    void xrayImaging::processCapturedImage(int requestId, const QImage &img)
    {
    Q_UNUSED(requestId);
    patientRegistration x;
    x.connOpen();
    QSqlQuery query = QSqlQuery(x.db);
    QString patientId, lname;
    QByteArray inByteArray;
    QByteArray outByteArray,outByteArray2,outByteArray3;

    query.prepare("select PatientId,LastName from patientTable where PatientId = 567");
    if (query.exec()){
        while(query.next()){
           patientId = query.value(0).toString();
           lname = query.value(1).toString();
        }
    }
    
    int w=ui->label_first->width();
    int h=ui->label_first->height();
    QImage scaledImage = img.scaled(w,h,Qt::KeepAspectRatio,Qt::SmoothTransformation);
    
    QBuffer inbuffer(&inByteArray);
    inbuffer.open(QIODevice::WriteOnly);
    scaledImage.save(&inbuffer,"PNG");
    
    
    query.prepare("INSERT INTO imageTable (PatientId,LastName,Image) VALUES('"+patientId+"','"+lname+"',:imageData)");
    query.bindValue(":imageData", inByteArray);
    if( !query.exec() )
        qDebug() << "Error inserting Image into table:\n" << query.lastError();
    if( !query.exec( "SELECT Image from imageTable where PatientId = '"+patientId+"'"))
        qDebug() << "Error getting image from table:\n" << query.lastError();
      while(query.next()){
    //query.first();
     outByteArray = query.value(0).toByteArray();
     outByteArray2= query.value(1).toByteArray();
     outByteArray3= query.value(2).toByteArray();
       }
    QPixmap outPixmap = QPixmap();
    QPixmap outPixmap2 = QPixmap();
    QPixmap outPixmap3 = QPixmap();
    
    outPixmap.loadFromData( outByteArray );
    outPixmap2.loadFromData( outByteArray2 );
    outPixmap3.loadFromData( outByteArray3 );
    
    x.connClose();
    ui->label_first->setPixmap(outPixmap);
    ui->label_second->setPixmap(outPixmap2);
    ui->label_third->setPixmap(outPixmap3);
    

    }

    void xrayImaging::on_pushButton_takePicture_clicked()
    {

    camera->setCaptureMode(QCamera::CaptureStillImage);
    camera->searchAndLock();
    imageCapture->capture();
    camera->unlock();
    

    }

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Your select query only asks for the Image column so currently outByteArray2 and outByteArray3 will get an empty byte array since the value method will return an invalid QVariant for the 1 and 2 indexes.

      Also, using your while loop, you are changing the value of the outByteArray(,2,3) each iteration but will only see the last one.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      deleted511D 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        Your select query only asks for the Image column so currently outByteArray2 and outByteArray3 will get an empty byte array since the value method will return an invalid QVariant for the 1 and 2 indexes.

        Also, using your while loop, you are changing the value of the outByteArray(,2,3) each iteration but will only see the last one.

        deleted511D Offline
        deleted511D Offline
        deleted511
        wrote on last edited by
        #3

        @SGaist Thanks for the response,

        The imageTable has a foreign key of patientId that is not unique and may return more than one image under that patientId which is why I thought if I used the while loop then the other images would be iterated through the query slots. How would you approach this problem?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          If you only want the latest three images then add a limit to your query, that way you'll avoid some useless workload.

          Then you can create a QVector with your three labels in it and update their content in the while loop.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          deleted511D 1 Reply Last reply
          1
          • SGaistS SGaist

            If you only want the latest three images then add a limit to your query, that way you'll avoid some useless workload.

            Then you can create a QVector with your three labels in it and update their content in the while loop.

            deleted511D Offline
            deleted511D Offline
            deleted511
            wrote on last edited by
            #5

            @SGaist Thanks I changed the query to:
            SELECT Image from imageTable where PatientId = '"+patientId+"' order by ImageId DESC limit 3;
            Which works because I played around with it in my DB browser. Also how would i update the labels in my QVector if I can only retrieve the first query.value(0) and all the rest of the values will return invalid QVariant for indexes 1 and 2. Basically I am only getting one of the images since the other indexes are invalid, so how would I retrieve the other two images in my QVector?

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              query.value(#) iterates over the columns (fields)
              while(query.next()) iterates over the rows (records)

                while(query.next()){
              //query.first();
               outByteArray = query.value(0).toByteArray();
               outByteArray2= query.value(1).toByteArray();
               outByteArray3= query.value(2).toByteArray();
                 }
              QPixmap outPixmap = QPixmap();
              QPixmap outPixmap2 = QPixmap();
              QPixmap outPixmap3 = QPixmap();
              
              outPixmap.loadFromData( outByteArray );
              outPixmap2.loadFromData( outByteArray2 );
              outPixmap3.loadFromData( outByteArray3 );
              

              becomes

              QPixmap outPixmap = QPixmap();
              QPixmap outPixmap2 = QPixmap();
              QPixmap outPixmap3 = QPixmap();
                if(query.next()){
               outByteArray = query.value(0).toByteArray();
              outPixmap.loadFromData( outByteArray );
                 }
                if(query.next()){
               outByteArray = query.value(0).toByteArray();
              outPixmap2.loadFromData( outByteArray );
                 }
                if(query.next()){
               outByteArray = query.value(0).toByteArray();
              outPixmap3.loadFromData( outByteArray );
                 }
              
              

              "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

              deleted511D 1 Reply Last reply
              2
              • VRoninV VRonin

                query.value(#) iterates over the columns (fields)
                while(query.next()) iterates over the rows (records)

                  while(query.next()){
                //query.first();
                 outByteArray = query.value(0).toByteArray();
                 outByteArray2= query.value(1).toByteArray();
                 outByteArray3= query.value(2).toByteArray();
                   }
                QPixmap outPixmap = QPixmap();
                QPixmap outPixmap2 = QPixmap();
                QPixmap outPixmap3 = QPixmap();
                
                outPixmap.loadFromData( outByteArray );
                outPixmap2.loadFromData( outByteArray2 );
                outPixmap3.loadFromData( outByteArray3 );
                

                becomes

                QPixmap outPixmap = QPixmap();
                QPixmap outPixmap2 = QPixmap();
                QPixmap outPixmap3 = QPixmap();
                  if(query.next()){
                 outByteArray = query.value(0).toByteArray();
                outPixmap.loadFromData( outByteArray );
                   }
                  if(query.next()){
                 outByteArray = query.value(0).toByteArray();
                outPixmap2.loadFromData( outByteArray );
                   }
                  if(query.next()){
                 outByteArray = query.value(0).toByteArray();
                outPixmap3.loadFromData( outByteArray );
                   }
                
                
                deleted511D Offline
                deleted511D Offline
                deleted511
                wrote on last edited by
                #7

                @VRonin Much love! Works out. Thanks

                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