PDA

View Full Version : Save images to database



jnk5y
5th May 2006, 01:13
QT4 windows

I would like to save an image file (jpg,bmp...) into an sqlite database as a BLOB. What format must it be in to save it to the database? I can't find anything that converts into a ByteArray.

Once that problem is solved I will then have to view the image form the database. So whatever format i save it in I will have to be able to put it back into QImage or QPixmap.

Thanks.

init2null
5th May 2006, 02:44
If you want Qt to decode and/or encode it, the format should be on this list (http://doc.trolltech.com/4.1/qimagewriter.html#supportedImageFormats).
If you just need the data from an image file, QFile::readAll() should be enough. If you want to encode the image in memory, you need the following:


QBuffer buffer;
QImageWriter writer(&buffer, "PNG");
writer.write(image);
QByteArray data = buffer.data();

To decode:



QByteArray array = variant.toByteArray();
QBuffer buffer(&array);
QImageReader reader(&buffer, "PNG");
QImage image = reader.read();

I'm assuming you know how to insert the QByteArray into the database and get it out, but If you any clarification, just ask.

jacek
5th May 2006, 13:48
It should be possible to convert between QVariant and QImage with a single line of code.

jnk5y
5th May 2006, 20:21
Thanks init2null. I actually did it a similar way. I used QFile::readAll() and stored that into the database. Then used QImage::loadFromData() to put into memory.

Now I want to view it and I want to display it with a few lineEdits and a couple of buttons in a QMainWindow. Unfortunately the widget I want to display it on is not giving me back it's proper size in order to scale the image. Do you know why it would do that?

Chicken Blood Machine
8th May 2006, 19:56
Thanks init2null. I actually did it a similar way. I used QFile::readAll() and stored that into the database. Then used QImage::loadFromData() to put into memory.

Now I want to view it and I want to display it with a few lineEdits and a couple of buttons in a QMainWindow. Unfortunately the widget I want to display it on is not giving me back it's proper size in order to scale the image. Do you know why it would do that?

Widgets do not always correctly report their sizes until just before they are shown (particularly when they are used in layouts). Defer your code until the showEvent() to get accurate widget sizes.