简体   繁体   English

QT TableView-显示图标

[英]QT TableView - Show Icons

I'm having problems, show Qicon in rows. 我遇到了问题,请按行显示Qicon。

now: 现在:

在此处输入图片说明

I would like to show: 我想展示:

在此处输入图片说明

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? 有人可以帮我吗?

Your problem seems to be in your understanding of all the Qt::ItemDataRoles 您的问题似乎在于您对所有Qt::ItemDataRoles理解

You should change your code to return QIcon for Qt::DecorationRole , not for Qt::BackgroundColorRole 您应该更改代码以为Qt::DecorationRole返回QIcon ,而不是为Qt::BackgroundColorRole返回QIcon

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM