Reputation: 4975
I am trying to implement my own QAbstractItemDelegate for a table view. I added a mouseover effect, however, I have two issues:
1. My Mouseover detection is buggy
2. The Item delegate needs additional repaints, I do not know how to trigger repaints.
I try to detect when the mouse hovers over my delegate with the following code in paint
:
void myClass::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if (painter->window().contains(option.widget->mapFromGlobal(QCursor::pos()))) {
// ...
}
}
This leads to the rectangle extending over other cells in columns after the actual column and detecting if the mouse is over those columns as well. I tried options.rect
as well, but this rectangle seemed to be shifted above my actual cell.
For some reason the widget is automatically repainted when the cursor enters the widget, but not when the mouse leaves. This results in the widget getting stuck in the hover state, unless I force a redraw by sliding another window across the area.
While I can detect when the mouse enters/leaves my widget witht the following code, I do not know how to trigger a repaint of my widget. Since I do not inherit from QWidget
I do not have update()
available, and I do not know how to obtain the QPainter
to call paint
. How do I trigger repaints?
bool myClass::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) {
switch(event->type()) {
case QEvent::Enter:
case QEvent::HoverEnter:
std::cout << "+" << index.row() << std::endl;
break;
case QEvent::Leave:
case QEvent::HoverLeave:
std::cout << "+" << index.row() << std::endl;
//paint(, option, index);
break;
}
return false;
}
Upvotes: 2
Views: 2632
Reputation: 4010
Check if mouse over you can like this:
bool hover = option.state & QStyle::State_MouseOver
Repainting is not the item delegate task but view's. So you should detect mouse leave event at your view and call update()
there. It will triggers repainting view's delegates.
Upvotes: 5