MousePressEvent goes to Parent when pop up is visible

  • I have little example here which shows this, if you have pop up menu on QWidget (context menu) and you do not click on menu rather click on widget, mousePressEvent() of QWidget gets called and this is breaking my application because this calls hell of other application processing. Any Idea if this is bug in Qt or what?

    What I expect is visible pop up menu should disappear and event should not be propagated to parent. but here pop up does disappear but even also goes to parent

    @#include <QtGui>
    class myWidget:public QMainWindow
    myWidget(QWidget *parent=0)
    connect(this,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(myContextMenu(const QPoint&)));

        virtual void mousePressEvent(QMouseEvent * me)


    qDebug()<<"Test: I should not get mousePress pop up was visible";
    public slots:
    void myContextMenu(const QPoint& pos)

                     QPoint globalPos = mapToGlobal(pos);
                     QMenu myMenu;
                     myMenu.addAction("My Menu Item 1");
                     myMenu.addAction("My Menu Item 2");


    #include "main.moc"

    int main( int argc, char ** argv)
    QApplication app( argc, argv );

    myWidget widget;;
    return app.exec&#40;&#41;;


    [edit: fixed typo in title, eddy]

  • Well, if you don't click on the context menu, but on some point that is both, outside the context menu and inside the widget, then you actually did click on the widget (rather than the context menu) and it's not very surprising that the widget gets a mousePressEvent(). I would say that this is the expected behavior. What you want, as far as I understand, is that all mouse events for the widget should be disabled while context menu is showing. That's what I would expect from a modal dialog. But a context menu is not modal dialog. Other applications show the same behavior. If, for example, I open a context menu in my web-browser (firefox) and then click on a hyperlink while the context menu is still showing, the context menu is closed and the link does get activated...

    (Though I wonder why this behavior does break your application)

  • This breaks because I have some functionality on mousePressEvent on widget, I expect when pop up is visible and mouse clicks on widget (not on popup) then pop up should simply disappear and nothing else.

    It happens exactly as I expect when I run my application on Mac but not on windows

  • Well, on the Windows platform the default/expected behavior is that a context menu does not block mouse click events for the underlying widget (only a modal dialog would do that). The behavior on Mac OS X may be different (can't check, because I don't have a Mac available), but this doesn't mean that the behavior on Windows is "wrong". You will probably have to implement your own workaround or change your code to work on Win32 too...

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.