在QCursor中使用自定义图像 [英] Use custom image in QCursor
问题描述
我有一个.bmp图像,我想将其用作GUI的光标. QCursor文档建议可以这样做(自己的位图,可以使用带有位图和掩码的QCursor构造函数,也可以使用以像素图作为参数的构造函数),但由于得到'TypeError:QCursor():参数1具有当我尝试将建议的模块与位图一起使用时,出现意外的类型'str'.应该怎么做?
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?
以下是产生所述错误的代码.该文档还建议将一个Alpha蒙版和其他两个值传递给QCursor,但我不确定这些是否必要,以及如果需要的话.
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()
推荐答案
如果它可以帮助任何使用Google搜索的人,并且可以为whatEverColor
设置一个透明颜色.在__init__
中:
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)
这篇关于在QCursor中使用自定义图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!