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. How to display icons in QTableView via a custom QAbstractItemModel?

How to display icons in QTableView via a custom QAbstractItemModel?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 10.9k 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.
  • A Offline
    A Offline
    a5med
    wrote on last edited by
    #1

    I'm building a custom QAbstractItemModel model.

    The first column contains icons, the second one - text.

    This is the code of the data method:

    @QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const
    {
    if(role != Qt::DisplayRole )
    return QVariant();

    int col = index.column();
    if (col == 0)
    {
        return iconProvider->icon(QFileIconProvider::Folder);
    }
    else if (col == 1)
    {
        return "TEXT";
    }
    

    }@
    But all I get in the resulting Table View is just text in the second column. There's no folder icon in the first column.

    I know I have to use Qt::DecorationRole, however I never receive it in the role parameter!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      a5med
      wrote on last edited by
      #2

      So silly of me. The solution is obvious:

      @QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const
      {
      int col = index.column();

          if (role== Qt::DecorationRole)
          {
              if (col == 0 )
              {
                  return iconProvider->icon(QFileIconProvider::Folder);
              }
          }
          else if (role == Qt::DisplayRole)
          {
              if (col == 1)
                          {
                          return "HELLO";
              }
          }
          else
          {
              return QVariant();
          }
      }@
      
      1 Reply Last reply
      0
      • N Offline
        N Offline
        neira1991
        wrote on last edited by
        #3

        Hello iconprovider not understand it? that data type is? thank you very much for your help

        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