Closing a QDialog launched from a mouseReleaseEvent RightClick causes a blank context menu to appear
-
29-05-2021 - |
سؤال
This has happened for every QDialog I pop that fires from either a mouseReleaseEvent on a QGraphicsItem or from a QContextMenu. I don't see what the issue is - the code is pretty simple..
...
void MyQGfxItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button() == Qt::RightButton)
{
MyDialog someDlg;
if(someDlg.exec())
{
}
}
}
...
As soon as the dialog is closed, either through OK or Cancel, an empty context menu appears where the event was triggered:
The blank item seems to control if my QDockWidget is hidden or visible.. Any idea what is going on? Numerous Google searches has left me stuck.
Edit - it looks like this only occurs if the Right mouse button is used..
المحلول 2
Ha! Got it. It looks like the QMainWindow grabs Right-click events after everything is finished. Calling this->setContextMenuPolicy(Qt::NoContextMenu) in the QMainWindow suppressed it.
نصائح أخرى
According to the docs for QEvent, you should accept() events you do not want propagated to the parent widget. So somewhere in your mouseReleaseEvent, you should just add
event->setAccepted(true);
or
event->accept();
Note that you might also need to put this code in your mousePressEvent, too.