PDA

View Full Version : Storing/retrieving a QIcon from a database



KShots
28th May 2008, 20:10
Hello all... I've got an application in which I want to be able to store a few icons in a database (so all clients can use the same icon set, regardless of location). I thought I had worked out a means of accomplishing this, but it appears not... I only get a corrupted image on restore. Basically, I convert the QIcon to a QPixmap, which I then convert to a QImage, which I then call the bits function on, which I then pass to the QByteArray constructor (after casting to a const char *).

Pretty convoluted, and I suspect that I may have misread the docs at some point along the way. Here's what I'm doing specifically in code:
Recording an icon:

q.prepare("UPDATE discs SET typeicon=:icon WHERE library=:id AND slot=:slot");
QImage image(m_typeIcon.pixmap(QSize(32, 32)).toImage());
QByteArray ba((const char *)image.bits(), image.numBytes());
q.bindValue(":icon", ba);where q is a QSqlQuery object and m_typeIcon is a QIcon object.

Reading an icon:

m_typeIcon = QIcon(QPixmap::fromImage(QImage((const uchar *)q.value(3).toByteArray().constData(), 32, 32, QImage::Format_RGB32)));Again, q is a QSqlQuery object, and the icon is in column 3 of a select statement.

The data itself is stored in the database as a varbinary(32000).

mcosta
29th May 2008, 15:34
To store a QImage in a QByteArray write


QByteArray bArray;
QBuffer buffer(&bArray);
buffer.open(QIODevice::WriteOnly);

QDataStream out(&buffer)
out << image;

buffer.close()


To read a QImage from a QByteArray write


QByteArray bArray;
QBuffer buffer(&bArray);
buffer.open(QIODevice::ReadOnly);

QDataStream in(&buffer)
in >> image;

buffer.close()

KShots
30th May 2008, 17:30
Thanks, I'll give this a try :)

MarkoSan
30th May 2008, 17:42
Or you could store in database PATH to picture, not picture itself, like I did ...

KShots
30th May 2008, 18:43
Or you could store in database PATH to picture, not picture itself, like I did ...

That wouldn't work - not all machines have access to the same path, but all machines will have access to the same database.

benacler
1st June 2008, 03:55
why don't you store a blob of a full png file then creates the icon from the png ?