Save and Load pictures in QT c++ from SQLite
-
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.
-
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
inaddBindValue
/bindValue
for yourQSqlQuery
. -
-
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. -
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();
-
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);
-
@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 emptyQPixmap
.