How to close dialog when mouse leaves a QWidget in the mainwindow (but not when it is over the dialog)
-
Hello,
I have a dialog, that shows at the mouse position and is related to the underlying widget (like a right click menu for the underlying widget)
myWidget* tool = new myWidget(this); myWidget->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint); QPoint p = this->mapToGlobal(QPoint(event->x(), event->y())); tool->setGeometry(p.x(), p.y(), 100 , 50); tool->show();
Now I want the dialog to close when the mouse leaves the underlying widget. However using leaveEvent() does not work, since as soon as the widgets appears the leave event gets triggered (which is totally what it should do)
So how can I detect if the mouse moves outside the underlying widget?
-
Install an eventFilter on your widget. Let your mainWindow handle the events and close your dialog when the event is a
QEvent::Leave
type.Edit:
You have to enablemouseTracking
first.
myTool->setMouseTracking(true);
-
Hi,
Then apply the reverse logic: if the enter event is not from your dialog or the underlying widget, hide it.
-
Maybe you can make use of
So if you leave your widget at
QPoint
p andp
belongs to your dialog, you ignore that event.Edit: Probably you dont even need
topLevelAt
for that. -
Thanks for the ideas, I got it working like this now:
bool myWidget::event(QEvent* e) { if(e->type() == QEvent::Leave) { QPoint view_pos(x(), y()); QPoint view_pos_global = mapToGlobal(view_pos); QPoint mouse_global = QCursor::pos(); if(mouse_global.x() < view_pos_global.x() || mouse_global.x() > view_pos_global.x() + width()) { closeMenu(); } else if(mouse_global.y() < view_pos_global.y() || mouse_global.y() > view_pos_global.y() + height()) { closeMenu(); } } return QWidget::event(e); }
6/6