首页 > 解决方案 > Qt C++ QStyledItemDelegate 子类 - 鼠标悬停在油漆上

问题描述

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

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

}

标签: c++qtqstyleditemdelegate

解决方案


好的,抱歉想通了。忘记在我的视口中添加鼠标跟踪

ui->table->setMouseTracking(true);

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


推荐阅读