Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Display BLOB Field in QTableview - using QSqlQueryModel
QtWS25 Last Chance

[SOLVED] Display BLOB Field in QTableview - using QSqlQueryModel

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 4.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    roahanramesh
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chris17
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • R Offline
        R Offline
        roahanramesh
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • R Offline
          R Offline
          roahanramesh
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            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.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • R Offline
              R Offline
              roahanramesh
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

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

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  roahanramesh
                  wrote on last edited by
                  #8

                  Thanks for the tips. IT worked.

                  1 Reply Last reply
                  0
                  • O Offline
                    O Offline
                    ottodev
                    wrote on last edited by
                    #9

                    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.

                    Greetings!

                    1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      ottodev
                      wrote on last edited by
                      #10

                      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.

                      Greetings!

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved