May 8, 2012

mprakash mprakash
Lab Rat
23 posts

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

  1. #include <QtGui>
  2. class myWidget:public QMainWindow
  3. {
  4.        Q_OBJECT
  5. public:
  6.        myWidget(QWidget *parent=0)
  7.        {
  8.               setContextMenuPolicy(Qt::CustomContextMenu);
  9.               connect(this,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(myContextMenu(const QPoint&)));
  10.  
  11.        }
  12.  
  13.  
  14.         virtual void mousePressEvent(QMouseEvent * me)
  15.    {
  16.       QWidget::mousePressEvent(me);
  17.  
  18.   qDebug()<<"Test: I should not get mousePress pop up was visible";
  19.    }
  20.        public slots:
  21.               void myContextMenu(const QPoint& pos)
  22.               {
  23.  
  24.                      QPoint globalPos = mapToGlobal(pos);
  25.                      QMenu myMenu;
  26.                      myMenu.addAction("My Menu Item 1");
  27.                      myMenu.addAction("My Menu Item 2");
  28.                      myMenu.exec(globalPos);
  29.               }
  30.  
  31. };
  32.  
  33. #include "main.moc"
  34.  
  35. int main( int argc, char ** argv)
  36. {
  37.     QApplication app( argc, argv );
  38.        
  39.     myWidget widget;
  40.        widget.show();
  41.     return app.exec();
  42. }

[edit: fixed typo in title, eddy]

3 replies

May 8, 2012

MuldeR MuldeR
Robot Herder
800 posts

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)

 Signature 

My OpenSource software at: http://muldersoft.com/

Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

Go visit the coop: http://youtu.be/Jay-fG9eaYk

May 8, 2012

mprakash mprakash
Lab Rat
23 posts

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

May 8, 2012

MuldeR MuldeR
Robot Herder
800 posts

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…

 Signature 

My OpenSource software at: http://muldersoft.com/

Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

Go visit the coop: http://youtu.be/Jay-fG9eaYk

 
  ‹‹ How to default to the smallest possible size for a QDockWidget?      Does QT application should be supported by DRI(Direct Rendering Infrastructure) ››

You must log in to post a reply. Not a member yet? Register here!