PDA

View Full Version : Help with the setCursor() method



Kyl196
6th November 2016, 11:12
Hi guys, I'm a newbie here. I started playing with Qt only a few weeks ago only in my free time, and now I'm stuck :D .

I am reimplementing a QMainWindow and some QGraphicsItem.

I have some QPushButtons and a QGraphicsView in the window. I need the cursor shape to change when the user clicks on a QPushButton, and that works properly by calling setCursor(Qt::ForbiddenCursor) and view->viewport()->setCursor(Qt::ForbiddenCursor) (if I call it only on the window, or on the view, the mouse doesn't change when it's on the graphicsView. I need to call it also on the viewport. Why?).

Anyway this works fine if I call the setCursor() on both. Now I need the cursor to go back to normal as soon as the user clicks on a QGraphicsItem. So I override the mousePressEvent in the QGraphicsItem class and do the changes I have to do for my program. At the end of the method I propagate the event to the parent through "QGraphicsItem::mousePressEvent(event);" and this works too, because the window receives the event. The problem is that, when I call setCursor(Qt::ArrowCursor) in the window class and view->viewport()->setCursor(Qt::ArrowCursor), the cursor doesn't change! When the cursor is over the view it has still a ForbiddenCursor shape, while when it is over the rest of the window it has correctly an ArrowCursor shape. So the problem is that the cursor won't go back to normal when it's over the view, even if I called view->viewport()->setCursor(Qt::ArrowCursor).

I've already tried to use unsetCursor(), to call the setCursor(Qt::ArrowCursor) both on the view and on the viewport but it doesn't work.
Strangely though the cursor changes properly back "calling the same methods" only if the user misclicks and instead of clicking on a QGraphicsItem he clicks on the view. In this case the cursor actually turns back to ArrowCursor shape even while on the view. That's pretty strange..

Could you help me please? If it's not clear enough I'll post the code.
Thanks for your help :)

P.S. I used QtCreator for the main window, so I actually refer to the view as ui->view. Maybe this has something to do with the problem. Thus I wanted to ask: is there a way to subclass the QGraphicsView and use my subclass with QtCreator?

anda_skoa
6th November 2016, 12:55
This behavior is a bit weird.

Have you verified that the setCursor(Qt::ArrowCursor) call is actually executed when you click on an item?

Cheers,
_

Kyl196
6th November 2016, 17:27
Yes, since it changes the cursor for everything else. It's just when it comes to the graphic view that it doesn't work.

I've just noticed that if I never click on the graphicsView nor on the graphicsItems, it works by setting only window->setCursor(): the cursor changes also on the view. But if I interact with the view before calling the window->setCursor(), it doesn't change the cursor anymore on the view, but only on the rest of the window. Might it be a focus problem?

I would subclass the view to see if it is somehow modifying the cursor but I can't since I added the view through QtDesigner...

anda_skoa
6th November 2016, 19:41
You can create your own subclass and then use "promote widget" in Designer to make it an instance of your widget.

Cheers,
_