I am using several instances of QQuickPaintedItem class that listen to hoverEnterEvent and hoverLeaveEvent, as well as mousePressEvent and mouseReleaseEvent. I have a need to select or deselect items when the cursor passes over them with the mouse button held down. However, the "hover" events are no longer handled when the button is held down. How can I fix it?
In this case, the sequence of actions is as follows:
- mouse button is clicked over first element.
- the cursor is dragged over the second element
- when the cursor leaves the second element, it is highlighted
- the same with the next element, until the mouse button is released.
'Item::Item( QQuickItem* parent ) : QQuickPaintedItem( parent ) { setAcceptedMouseButtons( Qt::LeftButton ); setAcceptHoverEvents( true ); }
void Item::mousePressEvent( QMouseEvent* e )
{
myStateHandler->mousePressed = true;
}
void Item::mouseReleaseEvent( QMouseEvent* e )
{
myStateHandler->mousePressed = false;
}
void Item::hoverLeaveEvent( QHoverEvent* e )
{
QQuickItem::hoverEnterEvent( e );
if( myStateHandler->mousePressed )
{
isSelected = !isSelected;
update();
}
}
void Item::paint( QPainter* p )
{
if(isSelected )
{
p->setPen( Qt::NoPen );
p->setBrush( QColor( 205, 205, 0 ) );
p->drawRect( boundingRect() );
}
}'
Thank you in advance.