Results 1 to 6 of 6

Thread: Display BLOB Field in QTableView - using QSqlQueryModel

  1. #1
    Join Date
    Dec 2011
    Location
    Chennai, India
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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

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

    Please provide me with any pointers.

    Thanks Once Again.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Display BLOB Field in QTableView - using QSqlQueryModel

    You will get the data in the blob column as a QVariant(QByteArray). Convert the variant to a QByteArray then use QPixmap::loadFromData()

  3. #3
    Join Date
    Dec 2011
    Location
    Chennai, India
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Display BLOB Field in QTableView - using QSqlQueryModel

    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.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Display BLOB Field in QTableView - using QSqlQueryModel

    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.

  5. #5
    Join Date
    Dec 2011
    Location
    Chennai, India
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Display BLOB Field in QTableView - using QSqlQueryModel

    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 );
    }

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Display BLOB Field in QTableView - using QSqlQueryModel

    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.

Similar Threads

  1. Replies: 10
    Last Post: 30th June 2011, 22:47
  2. Replies: 4
    Last Post: 16th June 2011, 14:49
  3. How to update BLOB field in a SQLite database ?
    By aircraftstories in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2011, 20:45
  4. BLOB item in QTableView
    By baray98 in forum Qt Programming
    Replies: 0
    Last Post: 22nd September 2009, 07:17
  5. Replies: 1
    Last Post: 14th September 2009, 08:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.