PDA

View Full Version : Store QPixmap in sqlite BLOB field



juliano.gomes
3rd January 2016, 20:49
Hello,

I'm trying to store an QPixmap picture in a sqlite BLOB field, but it not storing. I've already search on forum and google, but not shure about my problem. can someone help me?

only the picture do not store on table



QString file = QFileDialog::getOpenFileName(this, tr("Select an image"),".", tr("JPEG (*.jpg *jpeg)\n"
"PNG (*.png)\n"
"GIF (*.gif)\n"));
QImage image = QImageReader (file).read();
QPixmap pm = QPixmap::fromImage(image).scaled(ui->label1->width(), ui->label1->height());
ui->label1->setPixmap(pm);

QString description = "picture description";

QSqlDatabase con = QSqlDatabase::addDatabase("QSQLITE");
con.setDatabaseName(_DB);
con.open();

QSqlQuery query;
query.prepare("INSERT INTO pictures (picture, description) VALUES (?,?)"); // picture is a blob field
query.bindValue(0,pm);
query.bindValue(1,description);
query.exec();


thanks!
Juliano

anda_skoa
3rd January 2016, 22:38
First, you don't need to sue QImageReader explicitly, QImage::load will do that for you.

Secondly, what you want to do is to store the pixel data into the database.
So save the pixmap/image into a QByteArray and store that into the database.

See QPixmap::save() and QImage::save() with a QBuffer as the QIODevice.

Or consider storing the orignal file.

Cheers,
_

juliano.gomes
4th January 2016, 22:54
Yeah! Solved
Thanks my friend!