0

I'm having problems, show Qicon in rows.

now:

enter image description here

I would like to show:

enter image description here

my code. But the icon does not appear and there is no compiler error:

c

lass MySubClassedSqlTableModel : public QSqlTableModel
      {
          Q_OBJECT
          public:
             MySubClassedSqlTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase())
             : QSqlTableModel(parent,db) {;}
             QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const
             {
                if(role==Qt::BackgroundColorRole)
                {
                   const QVariant value(data(index,Qt::DisplayRole));

                                       if(value.toString()=="yes"){
                                          return QIcon(":/img/icons/yes.png");
                                       }else{
                                          return QIcon(":/img/icons/no.png");
                                       }
                }
                return QSqlTableModel::data(index,role);
             }
      }; 

Could someone help me?

user628298
  • 287
  • 2
  • 14

1 Answers1

-1

Your problem seems to be in your understanding of all the Qt::ItemDataRoles

You should change your code to return QIcon for Qt::DecorationRole, not for Qt::BackgroundColorRole

            if(role==Qt::DecorationRole)
            {
               const QVariant value(data(index,Qt::DisplayRole));

                                   if(value.toString()=="yes"){
                                      return QIcon(":/img/icons/yes.png");
                                   }else{
                                      return QIcon(":/img/icons/no.png");
                                   }
            }
            return QSqlTableModel::data(index,role);
RobbieE
  • 4,280
  • 3
  • 22
  • 36
  • And to do both? I need the background color and the icon. could help me? – user628298 Mar 15 '14 at 14:46
  • please could show how to implement code in Qt::DecorationRole and QT::BackgroundColorRole? :) – user628298 Mar 16 '14 at 15:44
  • By asking that question, you're implying that you don't really understand the code you've written or the Model-View architecture as a whole. I suggest you read the documentation, and more specifically [the purpose of item roles](http://qt-project.org/doc/model-view-programming.html#item-roles) – RobbieE Mar 16 '14 at 19:32