• 961阅读
  • 0回复

[讨论]继承QAbstractTableModel后,重写自己的模型出现的问题 [复制链接]

上一主题 下一主题
离线lwei24
 

只看楼主 倒序阅读 楼主  发表于: 2022-04-27
如题,在项目中,要在QTableView里的某一列插入某个图标,我写模型的代码如下:
  1. QVariant SmartModel::data(const QModelIndex &index, int role) const
  2. {
  3.     if(!index.isValid())
  4.         return false;
  5.     if(index.row() >= rowCount() || index.row() < 0 || index.column() >= columnCount() || index.column() < 0)
  6.         return false;
  7.     switch(role)
  8.     {
  9.     case Qt::DisplayRole:
  10.     {
  11.         if(index.column() == 0) //ID
  12.             return QVariant(m_recordList[index.row()].strIdx);
  13.         if(index.column() == 2) //Description
  14.             return QVariant(m_recordList[index.row()].strDescrition);
  15.         if(index.column() == 3)
  16.             return QVariant(m_recordList[index.row()].strCurrentValue);
  17.         if(index.column() == 4)
  18.             return QVariant(m_recordList[index.row()].strWorstValue);
  19.         if(index.column() == 5)
  20.             return QVariant(m_recordList[index.row()].strThreshold);
  21.         if(index.column() == 6)
  22.             return QVariant(m_recordList[index.row()].strRawValue);
  23.     }
  24.     case Qt::TextAlignmentRole:
  25.     {
  26.         return QVariant(Qt::AlignHCenter | Qt::AlignVCenter);
  27.     }
  28.     case Qt::DecorationRole:
  29.     {
  30.         if(index.column() == 1) //Status
  31.         {
  32. #if 0
  33.             QString strStatus = m_recordList[index.row()].strStatus;
  34.             if(strStatus.toInt() == 1)
  35.                 return QVariant(QIcon(":/image/GooderCircle16.png"));
  36.             else if(strStatus.toInt() == 2)
  37.                 return QVariant(QIcon(":/image/NormalCircle16.png"));
  38.             else if(strStatus.toInt() == 3)
  39.                 return QVariant(QIcon(":/image/DangerCircle16.png"));
  40.             else
  41.                 return QVariant(QIcon(""));
  42. #else
  43.             return QVariant();
  44. #endif
  45.         }
  46.     }
  47.         break;
  48.     default:
  49.         return QVariant();
  50.     }
  51.     return QVariant();
  52. }


问题:在插入图标的第一列,莫名其妙的出现一个数字132,我明明在模型里Qt::DecorationRole添加的是图标,但是它却显示图标加上132这个数字。为了弄清楚132这个数字是怎么插入第一列的,我把Qt::DecorationRole判断第一列的代码注释了,然后直接返回一个空的QVariant。结果,在第一列上还是会插入132.各位大佬,请问这是为啥呢?我明明没有赋值132或在模型里的第一列插入132,它会显示在我写的QTableView的第一列中。我定位分析了很久,没有找到问题出在哪里。请各位大佬帮忙看看,有什么办法可以解决这个问题吗?欢迎各位大佬指点,小弟感激不尽!!!
快速回复
限100 字节
 
上一个 下一个