1. Intro
I'm creating an application in Python 3.7 with PyQt5 for the GUI. I would like to customize the mouse cursors in the application.
Let's start from the standard cursor set in Qt5 as shown in a table here:
https://doc.qt.io/qt-5/qt.html#CursorShape-enum. You will notice that Qt5 has a dedicated Enum Qt::CursorShape
describing the role of the corresponding cursor. For example:
I would like to replace each Standard Qt cursor with a custom made one:
2. First approach
At first I tried something like this:
pixmap = QPixmap("C:/../my_arrow.png")
cursor = QCursor(pixmap, 32, 32)
QApplication.setOverrideCursor(cursor)
Unfortunately, this approach is not good for my purpose. From the documentation:
Application override cursors are intended for showing the user that the application is in a special state, for example during an operation that might take some time.
The override cursor will be displayed in all the application's widgets untilrestoreOverrideCursor()
or anothersetOverrideCursor()
is called.
In other words, using the setOverrideCursor()
approach has two downsides:
I would have to track manually to which role the mouse pointer should change, call
setOverrideCursor()
each time and feed it with the properQCursor()
.I'd need to track wherever Qt automatically calls
restoreOverrideCursor()
, because that effectively undoes my own changes. It would be a constant battle against Qt.
3. Second approach
My second approach was playing around with the setCursor()
function:
pixmap = QPixmap("C:/../Arrow.png")
cursor = QCursor(pixmap, 32, 32)
my_widget.setCursor(cursor)
I do this on the toplevel widget - the QMainWindow()
- such that the effect is applied on the whole application.
It works great, but it has one downside. This function only changes the "default cursor" (the pointing arrow) but that's it. All the special cursors are still the same.
In fact, I would like to do something like this:
# Note: 'mainwin' is the QMainWindow().
mainwin.setCursor( QCursor(QPixmap("C:/../Arrow.png"), 32, 32), Qt.ArrowCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../UpArrow.png"), 32, 32), Qt.UpArrowCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../Cross.png"), 32, 32), Qt.CrossCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../Wait.png"), 32, 32), Qt.WaitCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../IBeam.png"), 32, 32), Qt.IBeamCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../SizeVer.png"), 32, 32), Qt.SizeVerCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../SizeHor.png"), 32, 32), Qt.SizeHorCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../SizeBDiag.png"), 32, 32), Qt.SizeBDiagCursor )
mainwin.setCursor( QCursor(QPixmap("C:/../SizeFDiag.png"), 32, 32), Qt.SizeFDiagCursor )
...
Unfortunately, that's not how the setCursor()
function works.
Do you have a solution that matches best my purpose?
4. Resources
I've learned a lot from the following sources:
Unfortunately, none of them provided the solution to my problem. I just mention them here, because they are related to - but not the same as(!) - what I'm trying to do.