Qt C++QStyledItemDelegate子类-鼠标悬停在绘画上



我已经对QStyledItemDelegate进行了子分类(只是绘制函数(,并将其与数据的自定义QAbstractTableModel模型一起应用于我的QTableview。表格的单元格绘制正确,选中时也是如此,但鼠标悬停的颜色不正确。我想念什么?这是油漆功能。。单元格都是黑色的,选定的单元格变为绿色,但当鼠标悬停在任何单元格上时,我都不会得到红色。

void  Mydelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->save();
if (option.state & QStyle::State_MouseOver) {
painter->fillRect(option.rect, QColor(Qt::red));
} else if (option.state & QStyle::State_Selected) {
painter->fillRect(option.rect, QColor(Qt::green));
} else painter->fillRect(option.rect, QColor(Qt::black));
painter->restore();
}

好的,很抱歉弄明白了。忘记在我的视口中添加鼠标跟踪

ui->table->setMouseTracking(true);

很抱歉,请标记为已解决(如果没有用处,甚至删除我的帖子(

最新更新