通过重载QAbstractTableModel中的data方法
QVariant CTblModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
int col = index.column();
if (col == ledColIndex && role == Qt::DecorationRole)
{
return QIcon(":/images/light.png");
}
else
{
...
}
return QVariant();
}
鼠标点击灯泡亮
重载QTableView的 mousePressEvent(),通过indexAt方找到当前单击的index,再根据索引找到灯泡所在的列,具体实现如下:
[cpp] view plaincopyprint?void CTblView::mouseMoveEvent(QMouseEvent *event)
{
const QPoint &p = event->pos();
QModelIndex modelIndex = indexAt(p);
if (modelIndex.isValid())
{
int col = modelIndex.column();
int row = modelIndex.row();
if (col == ledColIndex)
{
pTblModel->setLight(row);
}
}
}