PDA

View Full Version : QCanvasView question



JimBrown
10th May 2007, 18:35
Using: Qt 3.2.3

Hi,

I have a legacy app that defines a subclass of QCanvasView named DCanvasView. It is
used as a viewing area in the QMainWindow derived class. DCanvasView defines
its own contentsMouseMoveEvent method which changes the cursor shape to reflect current
state (three different shapes) and display mouse coordinates in the status bar.

My problem is that the cursor is changing when it enters the scroll bars; I only want it
to change when it enter the viewport. (Actually, I used MS Spy++ and found that there is
about a one-pixel frame-like border which is the parent (DCanvasView) window, and fully
contained within it is the viewport, the two scroll bars, and the cornerWidget, and the
cursor is changing when it is over that border, even prior to passing over the scrollbar.)

contentsMouseMoveEvent only fires when the cursor enters the viewport area, so I am at a
loss to explain where in the code the cursor is changing shape when it enters the non-
viewport area of the CanvasView.

Why is contentsMouseMoveEvent firing only when the cursor is in the viewport area? How
can I handle mouse moves in the non-viewport area of the CanvasView?

Thank you,
Jim Brown

wysota
10th May 2007, 22:15
Using: Qt 3.2.3
You can mark that in your forum profile, so that you don't have to repeat that information in every thread.


Why is contentsMouseMoveEvent firing only when the cursor is in the viewport area?
Because it fires on the contents which is handled by the viewport and not by the whole widget.


How
can I handle mouse moves in the non-viewport area of the CanvasView?
Reimplement mouse events for the view (like mouseMoveEvent) and not its contents (contentsMouseMoveEvent).

I'm not sure if I understand your problem... First you write you want to handle only the viewport area and then you ask how to handle areas other than the viewport... If you want to adjust the cursor for the viewport, why not handle it through the viewport widget directly? You could reimplement enter and leave events for the viewport (or install an event filter) and use QWidget::setCursor to change the cursor for the viewport. Or you can probably do it without enter/leave events as well - after all, the cursor shape changes only when something actually happens in the scene, right?

JimBrown
11th May 2007, 22:20
Although the app was using contentsMouseMoveEvent, the cursor was changing shape when it crossed the QCanvasView frame going from the outside into the widget. I never did figure out how or why that happened. So I thought that if I could capture the mouse move for the whole view (not just the viewport), I could explicitly change the cursor to the PointingHandCursor when it was outside of the viewport area (in other words, when it was over the scroll bars). Using mouseMoveEvent instead of contentsMouseMoveEvent did the trick. Thanks for the tip.

JimBrown
11th May 2007, 22:34
Here's the code I used in case it helps anyone...


void MyCanvasView::mouseMoveEvent( QMouseEvent* e )
{
QPoint pos = e->pos();
QRect vpr = viewport()->geometry();

if ( vpr.contains(pos)
|| ( pos.x() < 2 && pos.y() < vpr.bottom()) // view's left frame edge
|| ( pos.y() < 2 && pos.x() < vpr.right() )) // view's top frame edge
{
if ( pos.x() == vpr.right()
|| pos.y() == vpr.bottom() )
{
// I'm leaving the viewport and heading for a scrollbar
setCursor( gloveCursor );
}
else
{
setCursor( forbiddenCursor );
}
}
else
{
// I'm in the view but not the viewport; i.e. scrollbar area
setCursor( gloveCursor );
}
}