[SOLVED] Call mouseMoveEvent only in widget
-
Hello,
I implemented my own QWidget and you should be able to drag something around in it. So I implemented the mouseMoveEvent and that works, but it is always called if I try to drag something around, no matter if the cursor is in the widget or not. Shouldn't it only be called if the event happens within the widget?
And another, related question: If I call event->pos() in mouseMoveEvent, shouldn't the coordinates be relative to my widget? Because if I call it, I always get the coordinates relative to the window.
I am using Qt 5.3.1 on Arch Linux 64bit.
Thanks
-
Try disabling "mouse tracking":http://qt-project.org/doc/qt-5/qwidget.html#mouseTracking-prop so you'll only receive the move event if a button is clicked. I guess you were looking for that.
Yes, the position returned is relative to your widget. Exactly, which widgets do you have, and how can you say the coordinates are not relative to the widget? Have you tried comparing the coordinates with the ones returned in globalPos() ?
-
I tried disabling mouse tracking but it didn't work. The problem is not that it always calles mouseMoveEvent if I move the mouse. I am already checking if the left mouse button really is pressed.
Right now, I only have one widget, my own one. The problem is, that you can resize the window and therefore, the window is bigger than my widget. If I click somewhere on the window where no widget at all is, the mouseMoveEvent of my widget is called anyway. That's what I would want to prevent.
I can tell that the coordinates are not relative to the widget because my widget is placed somewhere in the middle of the screen by my QGridLayout (which is also a bit odd, because I place it at row 0 and col 0). If I click at the corner of my widget, the coordinates aren't around (0, 0), they are the position of my mouse relative to the corner of the window. And globalPos gives me the coordinates relative to the corner of my screen, which doesn't help me much either.
-
Hi,
Events are handled top-down through your parent/children widgets. So if the parent of your widget has a mousemove and it doesn't accept the event when outside your widget, also those events are given to the children of that widget. So mousemovements on you parent widget also gets handled by your child widget.
Read the event docs: "here":http://qt-project.org/doc/qt-5/eventsandfilters.html -
Ah, ok. That clarifies a lot, thanks. But what would be the easiest way to prevent that? Do I have to check by myself if the cursor is in within my widget? And what about pos()? Do I have to calculate the position relative to my widget myself as well?
Thanks
-
I found the problem. When my window was resized, my widget was resized too and I didn't check that yet. This event was passed through as well I think. If I resize my widget to it's original size again, pos() as well as moseMoveEvent() work as expected. Thanks for the help, I think I understand the events better now.
-
Glad to be off help! Happy coding!