2
votes

I have sub-classed QWidget to draw on it using mouse. I use setCursor to change its cursor to cross shape. It is working fine but as soon as I press the mouse button on it (for example to draw freehand line), the cursor changes back to application cursor. Note that I do not want to use setOverrideCursor on mouseenter event for example because I want to change cursor only for this widget and not for entire application, and I have a better workaround (as follows) anyways.

My current solution is to use setCursor(cursor()); in my overridden mousePressEvent(QMouseEvent * event) and mouseDoubleClickEvent(QMouseEvent * event) The latter is because for some reason double click also changes the cursor to the application cursor for a moment! The workaround works :) but I would like to see if there is any better solution, that asks QT not to change the cursor at all.

I should add that drag/drop is not activated.

Here is some source code snippet as requested:

class MyWidget : public QWidget
{
    void paintEvent( QPaintEvent * /*event*/ );
    void resizeEvent( QResizeEvent * event );
    void mouseDoubleClickEvent ( QMouseEvent * event );
    void mousePressEvent( QMouseEvent* event );
    void mouseReleaseEvent( QMouseEvent* event );
    void mouseMoveEvent( QMouseEvent* event );
    void wheelEvent( QWheelEvent* event );
}

Then I override the following (for the workaround)

void MyWidget::mouseDoubleClickEvent(QMouseEvent * event)
{
    // ... do some other stuff ...
    // This is a workaround to prevent the cursor from changing
    setCursor(cursor());
    event->accept();
}
void MyWidget::mousePressEvent(QMouseEvent * event)
{
    // ... do some other stuff ...
    // This is a workaround to prevent the cursor from changing
    setCursor(cursor());
    event->accept();
}

To change cursor assuming that mywidget is instantiated with my class, I do this: mywidget->setCursor(Qt::CrossCursor) Again, it changes the cursor as expected when I hover over my control, but it changes back to the application cursor once I press the mouse button (thus the need for the above workaround)

2
If you set a specific cursor once (say, in the constructor), whenever the mouse passes over that widget, that is how the cursor should appear. Once the mouse exits the widget, the cursor should automatically change back to normal. You shouldn't have to do anything special to change it as the mouse enters, leaves or is clicked.Arnold Spence
Can you please provide some source code?Septagram
@ArnoldSpence Well I do use setCursor and it automatically changes the cursor as you said uppon enter, exit. However, the cursor is applied only when the mouse button is not pressed. As soon as I press the button the cursor changes back to the application cursor (if setOverrideCursor is called beforehand, it will be used otherwise the OS default is used).dashesy

2 Answers

1
votes
QApplication.setOverrideCursor(QtGui.QCursor(Qt.CrossCursor))

and when the QWidget closed, set back to the original cursor

0
votes

Ok I still have not found any answer for this, so here is the workaround:

void MyWidget::mouseDoubleClickEvent(QMouseEvent * event)
{
    // ... do some other stuff ...
    // This is a workaround to prevent the cursor from changing
    setCursor(cursor());
    event->accept();
}
void MyWidget::mousePressEvent(QMouseEvent * event)
{
    // ... do some other stuff ...
    // This is a workaround to prevent the cursor from changing
    setCursor(cursor());
    event->accept();
}