Qt-interest Archive, August 2004
BLOBS for Dummies
Message 1 in thread
Anybody have an example of writting a QPixmap as a blob to sql database
via a QCursor. I just don't get it.
thanks
-david
Message 2 in thread
On Tuesday 31 August 2004 01:24 pm, David Boosalis wrote:
> Anybody have an example of writting a QPixmap as a blob to sql database
> via a QCursor. I just don't get it.
Here's the code I use for reading a blob pixmap - presumably you can use this
logic for a write:
QSqlCursor cur("engine_info", true, mvpDatabase);
cur.select(QString("serial='%1'").arg(EngineSerial()));
QByteArray s = cur.value("picture").toByteArray();
QPixmap b(s);
return b;
Message 3 in thread
On August 31, 2004 12:24 pm, David Boosalis wrote:
> Anybody have an example of writting a QPixmap as a blob to sql database
> via a QCursor. I just don't get it.
I don't have an example using QCursor but I do with QSqlQuery and
fundamentally the code should be pretty similar. This is PROBABLY based on
someone else's code from this mailing list.
To save a bitmap to the database, you just bind the value using
QSqlQuery.bindValue(). No big deal there, nothing special, just use it as if
you were binding an integer or a QString. Thus, your problem is simply to
get your image into a bitmap initially. This is how I read from a file into
a bitmap:
QFile f(newFilename);
if (!f.open(IO_ReadOnly)) {
qWarning(QString("Unable to open picture file '%s' for
display.").arg(newFilename));
QMessageBox::information(this, "Unable to open",
tr("Unable to open picture file '%s' for
display.").arg(newFilename),
QMessageBox::Ok);
return;
}
this->byteArray = f.readAll();
byteArray is of course of type QByteArray.
Retrieving data is just as easy. My loadBitmap routine is:
void WidgetPictureControl::loadBitmap(const long long newId) {
try {
QString query;
QSqlQuery q(QSqlDatabase::database("oscommerce"));
query = QString("SELECT filename, data FROM images WHERE id =
%1").arg(newId);
q.exec(query);
if (!q.next()) {
throw DBExceptionSelect(__FILE__, __LINE__); // My own exception
class
}
this->filename = q.value(0).toString();
this->byteArray = q.value(1).toByteArray(); // Here's the key
transformation
this->id = newId;
displayBitmap(this->byteArray);
}
}
And displayBitmap is nothing special, but I resize pictures to better fit my
GUI:
void WidgetPictureControl::displayBitmap(const QByteArray &image) {
this->byteArray = image;
QPixmap bitmap(this->byteArray); // See, nothing special
this->labelPicture->setPixmap(bitmap);
// Adjust the height to keep perspective
if (bitmap.width() <= 140) {
this->labelPicture->setScaledContents(false);
} else {
this->labelPicture->setScaledContents(true);
this->labelPicture->setMaximumHeight(static_cast<int>(bitmap.height()
* (140.0 / bitmap.width())));
}
}
If you'd like any expansion on these topics, please let me know.
--
[ signature omitted ]