PDA

View Full Version : Move mouse over object



ToddAtWSU
27th September 2007, 14:28
How do I see if a mouse has moved over a QObject? I have a QObject, and any time the mouse moves over it, I want to change the appearance of the cursor and then as soon as it moves off the QObject, change the cursor back to its original state. What do I have to capture for this object? Thanks!

jacek
27th September 2007, 14:32
See QWidget::enterEvent() and QWidget::leaveEvent().

ToddAtWSU
27th September 2007, 14:38
But I don't inherit from QWidget, I inherit from QObject. How can I use this functionality since it is for QWidget only? Thanks.

jacek
27th September 2007, 14:41
I inherit from QObject. How can I use this functionality since it is for QWidget only?
If you don't have a widget, you can't move a mouse over it.

ToddAtWSU
27th September 2007, 14:54
I have a QGLWidget that is a plot. I then have numerous QObjects that I add to the QGLWidget. The one class I have is to display some sort of "marker line" on the plot and it has some OpenGL drawing code that it performs by setting up its viewport onto a specific portion of the QGLWidget. But this "marker line" is just a QObject. So I guess I can capture when the mouse is over the QGLWidget and go through the marker lines and if it the mouse is on top of one, then change the cursor. So this will work for me now that I think about it a little more.

Thanks!

ToddAtWSU
28th September 2007, 15:39
I have been thinking about this and came up with another problem. Sure I can check when the mouse enters the widget and when the mouse leaves the widget, but can I track when the mouse moves around the widget after entering it and before leaving it? Since I only want to change the mouse cursor over a portion of my widget, is there a way to track the mouse during its movement over the widget? Do I just need to turn on mouse tracking with the setMouseTracking function? Or do I need to do more things? Thanks!

momesana
28th September 2007, 15:49
The docs are your friend! ;)

void QWidget::mouseMoveEvent ( QMouseEvent (http://doc.trolltech.com/4.3/qmouseevent.html) * event ) [virtual protected]
This event handler, for event event, can be reimplemented in a subclass to receive mouse move events for the widget.
If mouse tracking is switched off, mouse move events only occur if a mouse button is pressed while the mouse is being moved. If mouse tracking is switched on, mouse move events occur even if no mouse button is pressed.
QMouseEvent::pos (http://doc.trolltech.com/4.3/qmouseevent.html#pos)() reports the position of the mouse cursor, relative to this widget. For press and release events, the position is usually the same as the position of the last mouse move event, but it might be different if the user's hand shakes. This is a feature of the underlying window system, not Qt.
http://doc.trolltech.com/4.3/qwidget.html#moveEvent

ToddAtWSU
28th September 2007, 16:08
No I don't need the Press event to change the cursor, because I only want the cursor to change if I move over the object. I don't want to require the user to click on the plot. Is this a correct though?

jacek
28th September 2007, 20:44
Do I just need to turn on mouse tracking with the setMouseTracking function? Or do I need to do more things?
You have to reimplement mouseMoveEvent() to be able to react to events and you have to enable mouse tracking to tell Qt that you want to receive those events.

ToddAtWSU
1st October 2007, 15:31
I have code re-implementing mouseMoveEvent( ) and it is almost working. I have 2 QGLWidgets on my screen and I only want to be able to track the mouse on the top one. This is working, except the coordinates are WAY off. When I move over the top QGLWidget, the y-position of my mouse after I convert it to ortho coordinates is > 100,000. But my ortho only goes from 0-125. But if I click on the plot, and then move the mouse around, everything converts correctly. Is there a way to make this plot always be the "active" widget when no buttons are pushed so I can properly convert the screen coordinates to ortho coordinates? Thanks!

jacek
1st October 2007, 16:38
How do you convert those coordinates? And where is that "top QGLWidget" placed exactly?

ToddAtWSU
1st October 2007, 18:02
How do you convert those coordinates?
I have a scale factor based on the width and height of the viewport in pixels, and the min and max of my ortho.

xScaleFactor = (mXMax - mXMin) / (mViewportRight - mViewportLeft);
yScaleFactor = (mYMax - mYMin) / (mViewportTop - mViewportBottom);
I then convert the x and y coordinates like this

double x = ( event->x( ) - getLeftMarginWidth( ) ) *
xScaleFactor + mXMin;
double y = ( height( ) - event->y( ) -
getBottomMarginHeight( ) ) * yScaleFactor + mYMin;
This is how I convert the coordinates, and when I print out the conversion, it's value is the ortho value I expect.

And where is that "top QGLWidget" placed exactly?
I have a QVerticalSplitter with 2 QGLWidgets in the splitter. I also have 2 toolbars left of the 2 QGLWidgets, and they are also placed in the splitter so I have something looking like

---------------------------- <-- Top Of Window
| Toolbar | QGLWidget |
---------------------------- <-- Splitter
| Toolbar | QGLWidget |
---------------------------- <-- Bottom Of Window

The top QGLWidget is the one at the top of the window. That picture above is a simple layout of what the main portion on the window looks like.

My problem is when I move over the top QGLWidget it seems like it is asking for the scale factors from the bottom QGLWidget and not the top QGLWidget. But once I click on the top QGLWidget, then everything works how I expect it to. Thanks for your continued help!

Edit:
Fixed the diagram above

jacek
1st October 2007, 22:09
My problem is when I move over the top QGLWidget it seems like it is asking for the scale factors from the bottom QGLWidget and not the top QGLWidget. But once I click on the top QGLWidget, then everything works how I expect it to.
Do event->x() and event->y() return the same values for the top widget in both cases or maybe sometimes you get the mouse position relative to that bottom QGLWidget?

ToddAtWSU
2nd October 2007, 15:54
Do event->x() and event->y() return the same values for the top widget in both cases or maybe sometimes you get the mouse position relative to that bottom QGLWidget?

Yes they return the same value. event->y( ) returns values between 0-185 and event->x( ) returns 79-955. Both before and after I click on the top QGLWidget.

jacek
2nd October 2007, 21:04
Yes they return the same value. event->y( ) returns values between 0-185 and event->x( ) returns 79-955. Both before and after I click on the top QGLWidget.
This means that other variables in that conversion formula must have wrong values. How and when do you set them?

ToddAtWSU
2nd October 2007, 21:20
This means that other variables in that conversion formula must have wrong values. How and when do you set them?

I set them upon creation of the QGLWidget. I have some other functions that get called by the constructor and create the data set that goes and calculates my mins, maxs, widths, and heights for all my different objects. But it looks like I might not be storing my mins and maxs like I thought I was...so I will look more deeply into this and let you know if this is what it is. Thanks a whole lot!!!

jacek
2nd October 2007, 21:58
I set them upon creation of the QGLWidget.
Remember that widget size isn't correct until the widget is shown for the first time.

ToddAtWSU
3rd October 2007, 15:53
The widget size wasn't causing my problem. I was forgetting to initialize my mYMin value after I added data. The data itself knew what the min was, but since I use it so frequently I had a variable in my other class to hold it, and I forgot to update it. But now that I initialized it, it works fine! Thanks for your help and I guess I should debug more first before blaming Qt!