2

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()
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Sadie LaBounty
  • 379
  • 1
  • 5
  • 23
  • 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 Apr 03 '17 at 12:00
  • I've added a basic GUI to the query - anything will do for the image in this context. – Sadie LaBounty Apr 03 '17 at 12:36
  • 1
    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 Apr 03 '17 at 13:03
  • Thanks - the cursor appears but, despite having removed the background in photoshop, a white background still appears. – Sadie LaBounty Apr 03 '17 at 13:41
  • 1
    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 Apr 03 '17 at 14:10
  • Yes you're right, BMP doesn't support transparency. Do you know if it's possible then to remove the background? = – Sadie LaBounty Apr 03 '17 at 14:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139782/discussion-between-laurent-g-and-jonathan-connell). – Laurent G Apr 03 '17 at 14:16

1 Answers1

3

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)
Sadie LaBounty
  • 379
  • 1
  • 5
  • 23
Laurent G
  • 397
  • 8
  • 16