Changing the cursor in a QGraphicsView
Asked Answered
S

2

9

I'm trying to change the cursor of a QGraphicsView while the ScrollHandDrag is on, but it doesn't seem to work. I can change the cursor if I disable the ScrollHandDrag but not while it's active, I don't see what I could possibly be doing wrong...

Bellow is a portion of code that reproduce the problem:

QApplication app(argc, argv);
QGraphicsScene scene;
QRect rectangle(-8, -4, 100, 100);
QPen pen(Qt::blue, 1, Qt::SolidLine);
scene.addRect(rectangle, pen);
scene.setBackgroundBrush(Qt::white);
QGraphicsView vue(&scene);
vue.setFixedSize(250, 250);
//vue.setDragMode(QGraphicsView::ScrollHandDrag);
vue.setCursor(Qt::CrossCursor);
vue.show();

return app.exec();
Stlaurent answered 8/6, 2012 at 16:32 Comment(0)
M
15

QGraphicsView will automatically change the cursor while dragging, but you can easily fix this by reimplementing a few functions:

class CoolView : public QGraphicsView
{
protected:
    void enterEvent(QEvent *event)
    {
        QGraphicsView::enterEvent(event);
        viewport()->setCursor(Qt::CrossCursor);
    }

    void mousePressEvent(QMouseEvent *event)
    {
        QGraphicsView::mousePressEvent(event);
        viewport()->setCursor(Qt::CrossCursor);
    }

    void mouseReleaseEvent(QMouseEvent *event)
    {
        QGraphicsView::mouseReleaseEvent(event);
        viewport()->setCursor(Qt::CrossCursor);
    }
};
Morna answered 8/6, 2012 at 17:54 Comment(2)
Is there a function to let the cursor to its default value? I've tried something like that: void MyQGraphicsView::mouseReleaseEvent(QMouseEvent *event) { QGraphicsView::mouseReleaseEvent(event); if (m_click) { viewport()->setCursor(Qt::CrossCursor); } else { viewport()->unsetCursor(); } } but instead of having a hand cursor, which is what I would expect, I have the arrow cursor. My question is: do I have to set all the different cursors or is there a function that would just undo the setCursor?Stlaurent
@Stlaurent You can comment out enterEvent entirely, then change mouseReleaseEvent so that it has viewport()->setCursor(Qt::OpenHandCursor);.Morna
A
4

From poking around in Qt's source code, it looks like they take control of that cursor when you enter drag mode and there's no way to stop them from trying.

The only workaround that I'm aware of is to use QApplication::setOverrideCursor() and QApplication::restoreOverrideCursor() which will set the cursor globally. Unfortunately this means you'd have to do a lot managing of when the mouse cursor enters/leaves your QGraphicsView in order to prevent your whole application from getting stuck with the same cursor everywhere.

It's also worth noting that the cursor is set at the viewport level, so it would be slightly more appropriate to do vue.viewport()->setCursor(Qt::CrossCursor)

Reference documentation:

QApplication::setOverrideCursor

QApplication::restoreOverrideCursor

QApplication::changeOverrideCursor

Acacia answered 8/6, 2012 at 16:45 Comment(1)
Okay, thank you for your answer. Also if you could point me in any direction on how to manage those cursors or link me something worth reading that would be much appreciated!Stlaurent

© 2022 - 2024 — McMap. All rights reserved.