Loading image from computer files save in database and display later
-
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(); }
-
@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.
-
@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 withQFile
and use its methods to read its content as aQByteArray
. Send that to your database to store as a BLOB. -
@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.
-
@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? -
how sure are you, that
IMAGEDATA
is actually of type BLOB ? -
@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....
-
Hi
Did you look over this example?
https://wiki.qt.io/How_to_Store_and_Retrieve_Image_on_SQLite -
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 .
;
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); }
-
Hi,
What exactly does not work ?