Background and icon in tableView

2

I'd like to know how do I add a tableView to the background and icone property? I can already add the Background is now missing the icon.

Code:

class tableView : public QSqlTableModel
  {
      Q_OBJECT
      public:
         tableView(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()=="Sim"){
                                      return QVariant(QColor("#d0ffe3")); //<<background 
                                  }else if (value.toString()=="Não"){
                                      return QVariant(QColor("#ffe3d0")); //<<background 
                                  }else if (value.toString()=="Neutro"){
                                      return QVariant(QColor("#fffad0")); //<<background 
                                  }
             }
            return QSqlTableModel::data(index,role);
         }
 };
    
asked by anonymous 27.03.2014 / 02:30

1 answer

3

When the view requests a DecorationRole, just return a QPixmap, which can work either a background image or an icon, or even return a QIcon:

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

   if(value.toString()=="Sim"){
      return QVariant(QColor("#d0ffe3")); //<<background 
   }else if (value.toString()=="Não"){
      return QVariant(QColor("#ffe3d0")); //<<background 
   }else if (value.toString()=="Neutro"){
      return QVariant(QColor("#fffad0")); //<<background 
   }
} elseif (role==Qt::DecorationRole) {
   return QPixmap( "/caminho/pra/imagem/de/teste.jpg" );

   // Depois você vai ter que adaptar ao seu critério de
   // onde vai pegar o icone desejado.
   // Pode usar a mesma lógica do bloco de cima, mas em vez
   // de QColor devolva um QIcon ou QPixmap
}
    
27.03.2014 / 03:44