Displaying more than one QImage from SQLite database.
-
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();
}
-
Hi,
Your select query only asks for the Image column so currently
outByteArray2
andoutByteArray3
will get an empty byte array since thevalue
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.
-
Hi,
Your select query only asks for the Image column so currently
outByteArray2
andoutByteArray3
will get an empty byte array since thevalue
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.
@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?
-
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.
-
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.
@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? -
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 ); }
-
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 ); }
@VRonin Much love! Works out. Thanks