May 30, 2012

a5med a5med
Lab Rat
2 posts

How to display icons in QTableView via a custom QAbstractItemModel?

 

I’m building a custom QAbstractItemModel model.

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

This is the code of the data method:

  1. QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const
  2. {
  3.     if(role != Qt::DisplayRole )
  4.         return QVariant();
  5.  
  6.     int col = index.column();
  7.     if (col == 0)
  8.     {
  9.         return iconProvider->icon(QFileIconProvider::Folder);
  10.     }
  11.     else if (col == 1)
  12.     {
  13.         return "TEXT";
  14.     }
  15. }

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!

2 replies

May 30, 2012

a5med a5med
Lab Rat
2 posts

So silly of me. The solution is obvious:

  1. QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const
  2.     {
  3.         int col = index.column();
  4.  
  5.         if (role== Qt::DecorationRole)
  6.         {
  7.             if (col == 0 )
  8.             {
  9.                 return iconProvider->icon(QFileIconProvider::Folder);
  10.             }
  11.         }
  12.         else if (role == Qt::DisplayRole)
  13.         {
  14.             if (col == 1)
  15.                         {
  16.                         return "HELLO";
  17.             }
  18.         }
  19.         else
  20.         {
  21.             return QVariant();
  22.         }
  23.     }

March 24, 2013

neira1991 neira1991
Lab Rat
1 posts

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

 
  ‹‹ [Solved] Scrollable Widget as container for other widgets      How to create a Tab layout like in Qt Creator? ››

You must log in to post a reply. Not a member yet? Register here!