Use custom image in QCursor

血红的双手。 提交于 2019-12-29 09:13:35

问题


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

回答1:


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)


来源:https://stackoverflow.com/questions/43180859/use-custom-image-in-qcursor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!