PDA

View Full Version : Display BLOB Field in QTableView - using QSqlQueryModel



roahanramesh
26th June 2013, 17:14
Hello,
Thanks for taking the time to checking my post, I am facing a bit of an issue and would like some pointers.

I have a QSqlQuerymodel that queries into a table that has BLOB Field, that contains image data in .PNG Format.

When i populate the table i get the Image column as ?PNG.

I realize that the view is unable to understand how to process binary data. So how do I go about solving this issue.


1. Should I sub-class QSqlQueryModel and implement data() function, that when it sees the index of the BLOB column to return a QPixmap

(or)
2. USe a Item delegate to render the view through a QLabel.

Please provide me with any pointers.

Thanks Once Again.

ChrisW67
26th June 2013, 23:04
You will get the data in the blob column as a QVariant(QByteArray). Convert the variant to a QByteArray then use QPixmap::loadFromData()

roahanramesh
22nd July 2013, 19:45
Thanks for the reply. Can you provide me with some extra information. I have implemented the data function. Now if my column index equals the image column then I can re-create the image from BLOB -> QByteArray -> QPixmap and display. Am I doing it right.

ChrisW67
22nd July 2013, 21:33
Yes. You can do the conversion in the model, perhaps returning the QPixmap in the Qt::DecorationRole, or in the delegate for a particular view.

roahanramesh
25th July 2013, 15:04
Below is the snippet of code that i have written... However, when I display the role of column 1 (BLOB Column) - I still get it as Display Role instead of Decoration Role. I am unsure as to how to set it. Please help

QVariant mysqlquerymodel::data(const QModelIndex &item, int role) const
{
QVariant value = QSqlQueryModel::data(item, role);

if(value.isValid() && (item.column()==1))
{
qDebug()<<"Role Assigned"<<role<<"And QVariant Type"<<value; // Gives Role 0 QVariant TYpe Qvariant(QByteArray)

QByteArray array;
//qDebug()<<"Initial Array Size"<<array.size();
array = value.toByteArray();
//qDebug()<<"ARray Size"<<array.size();
QPixmap pixmap;
pixmap.loadFromData(array,"PNG",Qt::AutoColor);
return pixmap;
}
return QSqlQueryModel::data( item, role );
}

ChrisW67
25th July 2013, 21:56
You are returning the same value for all roles in that column. You need to restrict yourself to the roles you wish the pixmap to be returned for, and return a null QVariant or the base model's value for other roles.

When queried for the Qt::DecorationRole you need to get the value for the EditRole, convert it, and return the QPixmap.