PDA

View Full Version : mousePressEvent on a QWidget not working



Narada
28th March 2016, 01:32
I am trying to implement mouse events, and the attached code works OK on a QDialog derived application. Why is it not working on a QWidget derived application?

anda_skoa
28th March 2016, 10:10
Not sure what you are trying to do, but why do you not want mousePressEvent() to be called?

You have quite some code in that method, so not getting it called sounds like the opposite of what you would want.

Cheers,
_

Narada
28th March 2016, 16:16
I was trying to understand how events, specific events like mouse and kb events and signals work. So I made the simplest possible application. I want to use this to do a pop up menu on another widget.

Do you have any other way to do it? From a conceptual point of view, this should work. Usually, when something simple like this does not work, I am not understanding something funndamental.

Narada

anda_skoa
28th March 2016, 17:18
From a conceptual point of view, this should work.
I guess it depends on what your goal is.
If you goal was to bypass the event handling of QWidget then yes this is how it works.

But the thread's title suggests that you wanted mousePressEvent to be called, which bypassing the necessary implementation in QWidget will make impossible, conceptually and actually.

So maybe you could clarify if you wanted to bypass the event widget event handling and are surprised that is actually worked, or of you intended to implement the mouse press event handler and think it should miraculously work even if you bypass the code that calls it.

Cheers,
_

Narada
28th March 2016, 17:28
Actually, I didn't mean to bypass the event handling of the QWidget. I simply wanted to use the event. The way I have used it is typical in many of the examples in the Qt books. The funny thing is instead of QWidget, if I use QDialog with exactly the same code, it works.

anda_skoa
28th March 2016, 18:25
Actually, I didn't mean to bypass the event handling of the QWidget.

But you do


return QObject::event(e);

You bypass the implementation of QWidget::event(), which contains (https://code.woboq.org/qt5/qtbase/src/widgets/kernel/qwidget.cpp.html#_ZN7QWidget5eventEP6QEvent) the type switch that calls the specific virtual function in QWidget.



The way I have used it is typical in many of the examples in the Qt books.

Maybe the mousePressEvent() override, I doubt these examples also bypass the very method that makes the calls to this methods.



The funny thing is instead of QWidget, if I use QDialog with exactly the same code, it works.
Interesting, that should not work either.
Maybe it was not "exactly the same code"?

Cheers,
_

Narada
29th March 2016, 15:42
Hello anda_skoa,

Thanks for sticking with the thread until I resolved the issue.

You are correct. I took the event() override out, and it works now. My QDialog implementation did not have that override, and that is why it worked.

One more step further in my learning how event(), mousePressEvent() ( and others), and signals interact with each other.

If there is a good discussion on these three areas I would be happy to read it.

Thank you again.

Narada