Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 views
in Technique[技术] by (71.8m points)

python - Use custom image in QCursor

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...