Qt 单元格(非行)悬停时的QStyledItemDelegate绘制事件
我有一个定制的QStyledItemDelegate,它在特定列中绘制QPixmap。当鼠标悬停在那个单元格上时,我想用不同的方式来画它 下面是我的绘制事件,它在未将鼠标置于状态时正确绘制单元格。但是,当我将鼠标悬停在行的任意位置时,它会更改颜色。只有当鼠标悬停在单元格上并带有pixmap时,我如何才能使其更改Qt 单元格(非行)悬停时的QStyledItemDelegate绘制事件,qt,qt5,qtableview,qtreeview,qstyleditemdelegate,Qt,Qt5,Qtableview,Qtreeview,Qstyleditemdelegate,我有一个定制的QStyledItemDelegate,它在特定列中绘制QPixmap。当鼠标悬停在那个单元格上时,我想用不同的方式来画它 下面是我的绘制事件,它在未将鼠标置于状态时正确绘制单元格。但是,当我将鼠标悬停在行的任意位置时,它会更改颜色。只有当鼠标悬停在单元格上并带有pixmap时,我如何才能使其更改 void myDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QMode
void myDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_ASSERT(index.isValid());
switch(index.column()) {
case DAY_COLUMN:
{
QSize btnSize = QSize(option.rect.height() * .9, option.rect.height() * .9);
QRect r = option.rect;
int x = r.right() - btnSize.width() - 10;
int y = r.top();
QRect btnRect = QRect(x, y, btnSize.width(), btnSize.height());
QPixmap pixmap(":/icons/edit.png");
// If hovered over, change color.
if(option.state & QStyle::State_MouseOver) {
auto mask = pixmap.createMaskFromColor(QColor("Black"), Qt::MaskOutColor);
pixmap.fill(QColor("Red"));
pixmap.setMask(mask);
}
painter->drawPixmap(btnRect, pixmap, pixmap.rect());
return;
}
/*.... draw other column(s) as appropriate ...*/
}
}
我在所有具有QTreeView的行上使用此委托。Qt 5.12这可能是因为默认情况下,QTreeView的选择行为是
QAbstractItemView::SelectRows
。您可以使用以下方法进行更改:
m_tree_view.setSelectionBehavior(QAbstractItemView::SelectItems);
查看更多信息: