pythonimagepyqtqcursor

Is it possible to create a custom cursor using PyQt?


I wanted to use a bigger version of the crosshair cursor for my program but it seems like i can't change the specs of the standard cursor shapes (e.g. QtCore.Qt.OpenHandCursor). That said, is it possible for me to create a customized version of a cursor instead? Like say, from a png image of a cursor?


Solution

  • The image should be opened first using PIL, converted to bitmap and then instantiated as a cursor. here's the code:

     from PIL import Image
        from PIL.ImageQt import ImageQt
        img = Image.open('cursor.png')
        imgQ = ImageQt(img)
        cursorBitmap = QtGui.QBitmap.fromImage(imgQ)
        CURSOR_NEW = QtGui.QCursor(cursorBitmap)
    

    edit: as @ekhumoro pointed out in the comments, it can be achieved by using Pixmap and combining it with QCursor, so it'd look like:

    CURSOR_NEW = QtGui.QCursor(QtGui.QPixmap('cursor.png'))
    

    if this causes an error it might be because the code cannot find the correct file, so the proper path might need to be added to the code