[SOLVED]Save and load image with database(Postgresql)
-
I have a problem with loading image from database to QLabel. My code looks like this:
Save a image to db:
@
QByteArray baPhoto;
QBuffer buffer( &baPhoto );
buffer.open( QBuffer::WriteOnly );
ui->lbImage->pixmap()->save( &buffer, "PNG" );QSqlQuery query; query.prepare( "UPDATE foo_table SET photo = :photo" ); query.bindValue( ":photo", baPhoto.toBase64() ) ); qDebug() << query.exec(); // return true
@
load image from db
@
// _val is a QVariant which I get from QRecord from photo field(bytea)
QPixmap* pixmap = new QPixmap();
qDebug() << pixmap->loadFromData( _val.toByteArray().toBase64() ); // return false
qDebug() << pixmap->loadFromData( _val.toByteArray() ); // return falseui->lbImage->setPixmap( *pixmap ); // empty QLabel
@
What is wrong?
-
Try this:
@QByteArray baPhoto;
QBuffer buffer( &baPhoto );
buffer.open( QBuffer::WriteOnly );
ui->lbImage->pixmap()->save( &buffer, "PNG" );QSqlQuery query;
query.prepare( "UPDATE foo_table SET photo = :photo" );
query.bindValue( ":photo", baPhoto ) );
qDebug() << query.exec(); // return true@without toBase64()
-
Yes - code without
@
toBase64()
@
saves pictures, but when I'm doing update I have a warning:
@
WARNING: nonstandard use of \ in a string literal
LINE 1: EXECUTE qpsqlpstmt_4 ('\211PNG\015\012\032\012\000\00...
^
HINT: Use the escape string syntax for backslashes, e.g., E'\'.
@
Can I ignore this warning? -
Ok I resolve this warning. Now I'm saving picture in this way:
@
// like in 1st post
QByteArray baPhoto;
QBuffer buffer( &baPhoto );
buffer.open( QBuffer::WriteOnly );
ui->lbImage->pixmap()->save( &buffer, "PNG" );QSqlQuery query;
query.prepare( "UPDATE foo_table SET photo = :photo" );
query.bindValue( ":photo", baPhoto.toBase64() ) ); // without toBase64() is warning
qDebug() << query.exec(); // return true
@
Load pic:
@
// we need convert from base64 to standard bytearray
pixmap->loadFromData( QByteArray::fromBase64( _val.toByteArray() ) );
@Thanx for help!