Reputation: 2645
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)
Upvotes: 2
Views: 4930
Reputation: 11
QApplication.setOverrideCursor(QtGui.QCursor(Qt.CrossCursor))
and when the QWidget closed, set back to the original cursor
Upvotes: 1
Reputation: 2645
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();
}
Upvotes: 0