QImage::loadFromData QByteArray
-
Hi, I'd need some help with this.
I have loaded a table from a database in a QTableView
This table contains some records with images - bitmaps. They are stored as bytes, of course.
So, I'd like to load and view those images somehow, for example, when I click on some record.I wrote these function.
@ void MainWindow::on_tableView_pressed(const QModelIndex &index)
{QImage *image = new QImage;
QByteArray barray = index.data(Qt::DisplayRole).toByteArray();
image->loadFromData(barray, "BMP"); ui->label->setPixmap(QPixmap::fromImage(*image)); ui->label->show();
}@
Since I've read that the QImage could act as a Buffer.
But this doesn't work.So I use a QBuffer:
@void MainWindow::on_tableView_pressed(const QModelIndex &index)
{QPixmap *pixmap = new QPixmap; QByteArray barray = index.data(Qt::DisplayRole).toByteArray(); QBuffer *buf = new QBuffer; buf->setBuffer(&barray); buf->open(QIODevice::ReadOnly); pixmap->loadFromData(buf->buffer(),"BMP",Qt::AutoColor); QGraphicsScene *scene = new QGraphicsScene(); scene->addPixmap(*pixmap); ui->graphicsView->setScene(scene);
}
@But this doesn't work either.
Am I doing something wrong?
The data was storaged in the database with a code written in Builder, and it was load like this:
@TByteDynArray Buffer=DataModuleCET->ADOQuery2->FieldByName("Data")->AsBytes;
TMemoryStream* Str = new TMemoryStream(); if(Buffer.Length>0) Str->Write((unsigned char*)&Buffer[0],Buffer.Length);
Str->Seek((int)0,(unsigned short)soBeginning);
IMat->Picture->Bitmap->LoadFromStream(Str);@Any suggesion in how to reproduce this in Qt C++?
-
Hi,
One thing is not clear, in your database, do you store only the raw image data (RGB components) ? Or is it a bmp (meaning with header etc... like a file) ?
-
Thank you for your response.
As you can see in the last piece of code, it's just a data stream from a Bitmap.
Actually, that's what I'm trying to do, reproducing that code as QT code. -
Since these are raw bytes, use the constructor of "QImage":http://qt-project.org/doc/qt-4.8/qimage.html#QImage-5 to build it.
Also you don't need to create a new QPixmap, use:
@scene->addPixmap(QPixmap::fromImage(image))@Hope it helps
-
as SGaist already asked: what is the datatype of the stored binary in your database? The code doesn't clears it up.
@DataModuleCET->ADOQuery2->FieldByName("Data")->AsBytes;@Is it really stored in the "BPM file format":http://en.wikipedia.org/wiki/BMP_file_format?
-
Thank you SGaist, but I have posted 2 ways of doing it, one with Pixmap, and anotherone without Pixmap.
I use that only for buffering purposes, but it's not working anyway.raven-worx, as you can see in the last line of the old code:
@IMat->Picture->Bitmap->LoadFromStream(Str);@
it's just a BMP in a Stream format, within a database.
-
Bitmap doesn't necessarily mean that's its really a BMP ... especially when i don't know the framework you are using.
Seems the QByteArray valid?
What happens when you write the QByteArray to a file and check if you can open? -
I also tried to write that data in a file, but no success.
What I realized debugging is that the "Loadfromdata" function is not working with the data I'm inserting.@ IMat->Picture->Bitmap->LoadFromStream(Str);@
The IMat is a TImage from C++ Builder libraries.
The memory Stream contains the Buffer from the query of the Database:@TByteDynArray Buffer=DataModuleCET->ADOQuery2->FieldByName("Data")->AsBytes;@
-
Then are you sure your are not getting only the RGB data of the image ?
-
As it's not working, I'm not sure of anything J
What would be the solution if it were the RGB data? -
If you have somewhere the image size (width, height), you can use QImage giving the proper image format and then make a QPixmap from it
-
Thank you for your help SGaist, I got stuck since I've discovered that the data storaged in that database is coded in a C++ Builder "whoknowshow" system.
I quit and try to find an alternative way.