在QCursor中使用自定义图像

问题描述 投票:2回答:1

我有一个.bmp图像,我想用作我的GUI的光标。 QCursor Documentation建议这是可能的(“使用你自己的位图创建一个游标,要么使用带有位图和掩码的QCursor构造函数,要么使用pixmap作为参数的构造函数”)但我似乎无法得到它因为我得到'TypeError:QCursor():当我尝试将建议的模块与我的位图一起使用时,参数1具有意外类型'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()
python image pyqt qcursor
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)
© www.soinside.com 2019 - 2024. All rights reserved.