开发者

Save and Retrieve an Image using Qt

In fact I found some useful ar开发者_运维知识库ticle related to my issue, but not exactly, I will try detail my problem

I have a table with tow columns id as integer and pix as blob

I have a form with label and two buttons open and save

I try to open a picture into label, then I have to save into database table when I click save

Also, I'm looking to retrieve the image again to label if I added navigation buttons as next and previous.

Any help please ?


Here's a short example for saving a QImage into the database:

// Converting image to byte array:
QByteArray bytes;
{
    QBuffer buffer (&bytes);
    buffer.open (QBuffer::WriteOnly);
    image.save (&buffer, "PNG");
}

// Writing data into the database:
QString id_string = id == -1 ? "NULL" : QString::number (id);
QSqlQuery query ("REPLACE INTO images "
                 "(id, image) VALUES (:id, :image)");
query.bindValue (":id", id_string);
query.bindValue (":image", QString (bytes.toBase64()));
if (!query.exec()) throw some_exception;

So basically you need to convert an image into the byte array, and then just use SQL query to save it in the database. Take note that I'm working on this very issue in my program as we speak, so the code above may be imperfect. Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜