PDA

View Full Version : How I can use a "Mouse Leave" event inside QItemDelegate::editorEvent ?



rsilva
16th May 2011, 03:10
I'm using this code, but the editorEvent doesn't called in a "leave" event.
And the mouse move don't capture events outside the item rect



bool DownloadItemDelegate::editorEvent(
QEvent* event,
QAbstractItemModel* model,
const QStyleOptionViewItem& option,
const QModelIndex &index)
{
if (event->type() == QEvent::MouseButtonPress)
{
QMouseEvent* e = (QMouseEvent*)event;

if (index.data(Qt::UserRole).toRect().contains(e->pos())) // button "resume"
{
model->setData(index, true, Qt::UserRole + 1); // button pressed (visual-only | button style = sunken)
model->setData(index, true, Qt::UserRole + 2); // button pressed (is clicked?)
return true;
}
}
else if (event->type() == QEvent::MouseButtonRelease)
{
QMouseEvent* e = (QMouseEvent*)event;

if (index.data(Qt::UserRole).toRect().contains(e->pos()))
{
model->setData(index, false, Qt::UserRole + 1); // button style = enabled

if (index.data(Qt::UserRole + 2).toBool()) // is clicked == true | emit resume();
{
//emit resume_pressed;
}

return true;
}

model->setData(index, false, Qt::UserRole + 2); // clear this flag
}
else if (event->type() == QEvent::MouseMove)
{
QMouseEvent* e = (QMouseEvent*)event;

if (!index.data(Qt::UserRole).toRect().contains(e->pos())) // if exit button rect, button style = enabled
{
model->setData(index, false, Qt::UserRole + 1);
return true;
}
else if (index.data(Qt::UserRole + 2).toBool()) // if enter button rect, and, the checked flag is true, button style = sunken
{
model->setData(index, true, Qt::UserRole + 1);
return true;
}
}

return false;
}


This handles the basics of a QPushButton that I need, but, how can I check if the mouse leave the item region ?
I need to check this event to clear the flags of the button.

wysota
17th May 2011, 01:50
You will not catch a leaveEvent this way as the cursor is not over the item anymore so a different item will start receiving events. That's your clue that the cursor has left the item.

veiovis
6th August 2015, 16:39
So, the solution is to remove the pressed flag from the event function of all other widgets, when they get an event? Does not seem right.

veiovis
7th August 2015, 13:37
with the MouseOver option flag like described here it works: http://www.qtcentre.org/threads/13146-QItemDelegate-enter-event