I am trying to use a combination of custom cursors and predefined cursors for my QGraphicsView. In my implementation, we created the concept of “modes” for presentation. This means that depending on what kind of "mode" the user is in, different things will be done when you click the left mouse button or drag to the left. In any case, none of them is a problem, just a context.
The problem occurs when I try to change the cursor for each mode. For example, for mode 1 we want to show a regular arrow cursor, but for mode 2 we want to use a custom pixmap. Apparently, we just call graphicsview->viewport()->setCursor(Qt::QArrowCursor) to go into mode 1 and graphicsview->viewport()->setCursor(our custom cursor) for mode 2. Except that it does not work at all.
Firstly, the cursor does not change to the user cursor. This is the first problem. However, if through another operation the drag-and-drop mode of the graphical representation is set to ScrollHandDrag, the cursor will switch to the user cursor after the completion of the drag-and-drop operation. Weird But the plot thickens ... As soon as we switch to the custom cursor, it can never be returned back to ArrorCursor, no matter how much we call setCursor(Qt::QArrowCursor) . it also doesn't seem like I'm calling setCursor in the viewport or the graphical representation itself.
So, just for fun, I added a call to graphicsview->unsetCursor() just before we want to change the cursor and at least straighten out the second problem. The cursor changes just fine while we do a little HandDragging between them. Better, but certainly not optimal. However, it should be noted that running unsetCursor in the viewport does not work. this is absolutely necessary to do on the graphical representation - regardless of what we position the cursor in the viewport.
To completely solve the problem, I added these two lines after setting the cursor:
graphicsview->setDragMode(QGraphicsView::ScrollHandDrag); graphicsview->setDragMode(QGraphicsView::NoDrag);
What a job, but you gads !! So, something magical happens inside these two methods that fix the problem, but looking at the code, I don’t see that. Something has to do with the fact that the drag and drop mode changes the cursor that I imagine.
Just for completeness, I should also mention that the thing that causes the mode change is QPushButton, which was added to the scene using QGraphicsScene->addWidget() . I do not know if this is related to this, but you never know.
I hope someone can clarify why I need to make these seemingly random calls. I do not think that I am doing something wrong. Thanks in advance for any help.
EDIT: Here is an example with cursor patches as described above. You can view and / or download them from the link below. There was little time to insert here. I have included the frames around which the cursors change, because it is funny to me that this is important somehow.
https://gist.github.com/712654
The code where the problem is located is located in MyGraphicsView.cpp, starting at line 104. Here is the cursor in the graphical representation. This is exactly as described above.
Keep in mind that cursors work with very ugly corrections - more or less. Without these lines, you will see very clearly the problems listed in the message above.
Also included in the link, this is all the code for mainWindow that uses the view, etc. The only thing missing is the images I use. But the images themselves do not matter, any 16x16 png will do.