Loading image from computer files save in database and display later
-
@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? -
@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.
-
how sure are you, that
IMAGEDATA
is actually of type BLOB ? -
@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.
@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 ?
-
@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: yourQPixmap 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); }
@HoMa said in Loading image from computer files save in database and display later:
QPixmap pixmap("../sqlPixmap/image.jpg"); QByteArray ba; QBuffer bu(&ba); bu.open(QIODevice::WriteOnly); pixmap.save(&bu);
Please check if the first line really works. Relaitve paths are not good since you don't know the current working directory of your program.
-
@JonB thanks people for your help,after going through documentation and one of example which I was given a here ,I found out that the code below is one having a problem,it was storing a file in a data base as a file path and not as Byte.
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); }``` So I had to change to something like this and it worked.
QByteArray HOME::bigimage;
void HOME::on_commandLinkButton_clicked()
{
// QByteArray bigimage;
QString filename=QFileDialog::getOpenFileName(this,tr("Open image"),"/",tr("Image Files(.png;.jpg;*.bmp)"));Qfile file(filename)
if(!file.open(QIODevice::ReadOnly) return;
bigimage=file.readAll(); -
@HoMa said in Loading image from computer files save in database and display later:
QPixmap pixmap("../sqlPixmap/image.jpg"); QByteArray ba; QBuffer bu(&ba); bu.open(QIODevice::WriteOnly); pixmap.save(&bu);
Please check if the first line really works. Relaitve paths are not good since you don't know the current working directory of your program.
@Christian-Ehrlicher It really does. I change only the file name from what is here to ":memory:" (like in the line below, which is commented out) and the picture can no longer be retreived.
Here the code with all calls tested:
try { QPixmap pixmap("../sqlPixmap/image.jpg"); QByteArray ba; QBuffer bu(&ba); bu.open(QIODevice::WriteOnly); pixmap.save(&bu); if( pixmap.width ()<= 0 || pixmap.height () <= 0) throw "Loading image from file failed"; QSqlDatabase db =QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName ("../sqlPixmap/db.sqlite"); //db.setDatabaseName (":memory:"); if( ! db.open()) throw "database could not be opened"; QSqlQuery v("SELECT sqlite_version()"); Q_ASSERT(v.next()); qDebug() << "sqlite version " << v.value (0).toString(); QSqlQuery q ("CREATE TABLE t (c)"); // executes on creation q.prepare ("INSERT INTO t (c) VALUES (:pic)"); q.bindValue (":pic", ba); if( ! q.exec()) throw "could not store data"; ///////////////////////////////////////////// q.prepare ("SELECT c FROM t"); if( ! q.exec()) throw "failed in select query"; if( ! q.next()) throw "failed to move to data set"; QByteArray readBa =q.value (0).toByteArray (); if( 0 >= readBa.size ()) throw "returned data is empty"; QPixmap readPic; readPic.loadFromData(readBa); ui->lbl->setPixmap (readPic); ui->lbl->setScaledContents (true); } catch(const char* error) { qDebug() << error; return; }
-
@JonB thanks people for your help,after going through documentation and one of example which I was given a here ,I found out that the code below is one having a problem,it was storing a file in a data base as a file path and not as Byte.
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); }``` So I had to change to something like this and it worked.
QByteArray HOME::bigimage;
void HOME::on_commandLinkButton_clicked()
{
// QByteArray bigimage;
QString filename=QFileDialog::getOpenFileName(this,tr("Open image"),"/",tr("Image Files(.png;.jpg;*.bmp)"));Qfile file(filename)
if(!file.open(QIODevice::ReadOnly) return;
bigimage=file.readAll();@GREYON
Not sure what happened to yourQPixmap pixmap=QPixmap(filename);
line now. After reading aQPixmap
from a file (or elsewhere) you can/should always check bool QPixmap::isNull() const. That would cover e.g. "file not found" as well as "error reading contents of file as pixmap". -
@JonB thanks people for your help,after going through documentation and one of example which I was given a here ,I found out that the code below is one having a problem,it was storing a file in a data base as a file path and not as Byte.
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); }``` So I had to change to something like this and it worked.
QByteArray HOME::bigimage;
void HOME::on_commandLinkButton_clicked()
{
// QByteArray bigimage;
QString filename=QFileDialog::getOpenFileName(this,tr("Open image"),"/",tr("Image Files(.png;.jpg;*.bmp)"));Qfile file(filename)
if(!file.open(QIODevice::ReadOnly) return;
bigimage=file.readAll();This is the correct one
QByteArray HOME::bigimage; void HOME::on_commandLinkButton_clicked() { // QByteArray bigimage; QString filename=QFileDialog::getOpenFileName(this,tr("Open image"),"/",tr("Image Files(.png;.jpg;*.bmp)")); Qfile file(filename) if(!file.open(QIODevice::ReadOnly) return; bigimage=file.readAll();
-
@Christian-Ehrlicher It really does. I change only the file name from what is here to ":memory:" (like in the line below, which is commented out) and the picture can no longer be retreived.
Here the code with all calls tested:
try { QPixmap pixmap("../sqlPixmap/image.jpg"); QByteArray ba; QBuffer bu(&ba); bu.open(QIODevice::WriteOnly); pixmap.save(&bu); if( pixmap.width ()<= 0 || pixmap.height () <= 0) throw "Loading image from file failed"; QSqlDatabase db =QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName ("../sqlPixmap/db.sqlite"); //db.setDatabaseName (":memory:"); if( ! db.open()) throw "database could not be opened"; QSqlQuery v("SELECT sqlite_version()"); Q_ASSERT(v.next()); qDebug() << "sqlite version " << v.value (0).toString(); QSqlQuery q ("CREATE TABLE t (c)"); // executes on creation q.prepare ("INSERT INTO t (c) VALUES (:pic)"); q.bindValue (":pic", ba); if( ! q.exec()) throw "could not store data"; ///////////////////////////////////////////// q.prepare ("SELECT c FROM t"); if( ! q.exec()) throw "failed in select query"; if( ! q.next()) throw "failed to move to data set"; QByteArray readBa =q.value (0).toByteArray (); if( 0 >= readBa.size ()) throw "returned data is empty"; QPixmap readPic; readPic.loadFromData(readBa); ui->lbl->setPixmap (readPic); ui->lbl->setScaledContents (true); } catch(const char* error) { qDebug() << error; return; }
@HoMa said in Loading image from computer files save in database and display later:
QPixmap pixmap("../sqlPixmap/image.jpg");
db.setDatabaseName ("../sqlPixmap/db.sqlite");As @Christian-Ehrlicher commented, you simply should not use a relative path here, as you just do not know where that will be. See my https://forum.qt.io/topic/138832/export-qtableview-to-pdf-with-a-push-button/8 to another user on the same matter.
-
@HoMa said in Loading image from computer files save in database and display later:
QPixmap pixmap("../sqlPixmap/image.jpg");
db.setDatabaseName ("../sqlPixmap/db.sqlite");As @Christian-Ehrlicher commented, you simply should not use a relative path here, as you just do not know where that will be. See my https://forum.qt.io/topic/138832/export-qtableview-to-pdf-with-a-push-button/8 to another user on the same matter.
@JonB Thx for your efforts - I appreciate that. But this is a test program for the single purpose of playing around with sqlite, blobs (and meanwhile "in memory"). Anyone who wants to use the code will have to modify the path to the picture - no matter if it is absolute or relative. For this reason I think it makes no difference in this specific case.
-
@Christian-Ehrlicher It really does. I change only the file name from what is here to ":memory:" (like in the line below, which is commented out) and the picture can no longer be retreived.
Here the code with all calls tested:
try { QPixmap pixmap("../sqlPixmap/image.jpg"); QByteArray ba; QBuffer bu(&ba); bu.open(QIODevice::WriteOnly); pixmap.save(&bu); if( pixmap.width ()<= 0 || pixmap.height () <= 0) throw "Loading image from file failed"; QSqlDatabase db =QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName ("../sqlPixmap/db.sqlite"); //db.setDatabaseName (":memory:"); if( ! db.open()) throw "database could not be opened"; QSqlQuery v("SELECT sqlite_version()"); Q_ASSERT(v.next()); qDebug() << "sqlite version " << v.value (0).toString(); QSqlQuery q ("CREATE TABLE t (c)"); // executes on creation q.prepare ("INSERT INTO t (c) VALUES (:pic)"); q.bindValue (":pic", ba); if( ! q.exec()) throw "could not store data"; ///////////////////////////////////////////// q.prepare ("SELECT c FROM t"); if( ! q.exec()) throw "failed in select query"; if( ! q.next()) throw "failed to move to data set"; QByteArray readBa =q.value (0).toByteArray (); if( 0 >= readBa.size ()) throw "returned data is empty"; QPixmap readPic; readPic.loadFromData(readBa); ui->lbl->setPixmap (readPic); ui->lbl->setScaledContents (true); } catch(const char* error) { qDebug() << error; return; }