[SOLVED] Display BLOB Field in QTableview - using QSqlQueryModel

  • 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


    1. USe a Item delegate to render the view through a QLabel.

    Please provide me with any pointers.

    Thanks Once Again.

  • I would go with the item delegate.

    The first idea might work too, make sure to return the QPixmap for the DecorationRole instead of DisplayRole.
    @if(role==Qt::DecorationRole && index.column==pixmapColumn)
    //create pixmap from data
    return pixmap;

  • Thanks for the information, but can you provide me with an example on how to go about it

  • 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;
    return pixmap;
    return QSqlQueryModel::data( item, role );

  • Moderators

    you should subclass "QStyledItemDelegate":http://qt-project.org/doc/qt-4.8/qstyleditemdelegate.html and reimplement sizeHint() and paint().

    In sizeHint() check the index for your BLOB column and return the size of your pixmap, else return the base class implementation.
    In the paint() method again check for your BLOB column and just draw the pixmap, else again call the base class implementation.

  • Thanks Raven-worx,
    Just of curiosity. If I go by the QStyledItemDelegate, do I need to implement a QLabel as well. I am a beginner, your help will be truly appreciated.

  • Moderators

    no...only for your BLOB column... if you call the base class implementation of QStyledItemDelegate you are fine for the rest of the columns.

  • Thanks for the tips. IT worked.

  • Just a note: You don't need to use delegates for this, just roles.

    The delegate will be needed if you want to edit those cells.


  • Just a note: You don't need to use delegates for this, just roles.

    The delegate will be needed if you want to edit those cells.


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.