2

我有一个 .bmp 图像,我想将它用作我的 GUI 的光标。QCursor文档表明这是可能的(“要使用您自己的位图创建光标,请使用采用位图和掩码的 QCursor 构造函数或采用像素图作为参数的构造函数”)但我似乎无法得到当我尝试将建议的模块与我的位图一起使用时,它会在我得到“TypeError: QCursor(): argument 1 has unexpected type 'str'' 时工作。这应该怎么做?

下面是产生上述错误的代码。文档还建议将 alpha 掩码和其他两个值传递给 QCursor,但我不确定这些是否有必要以及它们应该是什么。

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()
4

1 回答 1

3

如果它可以帮助任何人在此处搜索,并提供您可以将值设为whatEverColor透明颜色。在__init__

pm = QtGui.QPixmap('image.bmp')
bm = pm.createMaskFromColor(whatEverColor, Qt.MaskOutColor)
pm.setAlphaChannel(bm)
cursor = QtGui.QCursor(pm)
self.setCursor(cursor)
于 2017-04-03T18:30:30.143 回答