2
votes

I have a .bmp image that I would like to use as a cursor for my GUI. The QCursor Documentation suggests that this is possible ("To create a cursor with your own bitmap, either use the QCursor constructor which takes a bitmap and a mask or the constructor which takes a pixmap as arguments") but I can't seem to get it to work as I get 'TypeError: QCursor(): argument 1 has unexpected type 'str'' when I try to use the suggested module with my bitmap. How should this be done?

Below is a code that produces said error. The docs also suggest passing an alpha mask and two other values into QCursor but I am not sure if these are necessary and what they should be if they are.

import sys
from PyQt4 import QtGui, QtCore

QtGui.QCursor('image.bmp')

class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        cursor = QtGui.QPixmap('image.bmp')
        self.setCursor(QtGui.QCursor(cursor))
        self.home()

    def home(self):
        btn = QtGui.QPushButton("Quit", self)
        btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        btn.resize(100,100)
        btn.move(100,100)
        self.show()


def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

run()
1
Could you give us some code to chew ? As a bet I'd say you are giving the path string to your bmp directly to the QCursor constructor. If so, try calling the QPixmap constructor in between. - Laurent G
I've added a basic GUI to the query - anything will do for the image in this context. - Sadie LaBounty
Thanks for the code. Could you instanciate a QPixmap with the 'image.bmp' path, give it to the QtGui.QCursor constructor and give this latter to a widget, let us say 'self' in init, via setCursor ? - Laurent G
Thanks - the cursor appears but, despite having removed the background in photoshop, a white background still appears. - Sadie LaBounty
Uh. How did you try to do that ? I don't remember BMP can encode transparency, but googling I read otherwise. Can you update the code ? - Laurent G

1 Answers

3
votes

If it can help anyone googling to here, and provided you can give a value to whatEverColor to be the transparent color. In __init__ :

pm = QtGui.QPixmap('image.bmp')
bm = pm.createMaskFromColor(whatEverColor, Qt.MaskOutColor)
pm.setAlphaChannel(bm)
cursor = QtGui.QCursor(pm)
self.setCursor(cursor)