【问题标题】:QT TableView - Show IconsQT TableView - 显示图标
【发布时间】:2014-03-15 13:19:21
【问题描述】:

我遇到了问题,请按行显示 Qicon。

现在:

我想展示:

我的代码。但是图标没有出现,也没有编译错误:

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

有人可以帮我吗?

【问题讨论】:

    标签: c++ qt user-interface qtgui


    【解决方案1】:

    您的问题似乎在于您对Qt::ItemDataRoles的所有理解

    您应该更改代码以返回 QIconQt::DecorationRole,而不是 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);
    

    【讨论】:

    • 两者都做?我需要背景颜色和图标。可以帮助我吗?
    • 请说明如何在 Qt::DecorationRole 和 QT::BackgroundColorRole 中实现代码? :)
    • 通过问这个问题,您暗示您并不真正了解您编写的代码或整个模型视图架构。我建议你阅读文档,更具体地说是the purpose of item roles
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    相关资源
    最近更新 更多