【发布时间】:2016-02-17 07:18:08
【问题描述】:
我正在为 Maya 开发 PySide 界面,我想知道是否可以为按钮定义一个非矩形可点击区域。
我尝试使用 QPushButton 并扩展 QLabel 对象以获取按钮行为,但您知道是否有可能获得包含带有 alpha 通道的图片的按钮并使用该 alpha 定义按钮的单击区域?
如果您能指导我解决这个问题,我将不胜感激。 提前致谢。
我试过了……
from PySide import QtCore
from PySide import QtGui
class QLabelButton(QtGui.QLabel):
def __init(self, parent):
QtGui.QLabel.__init__(self, parent)
def mousePressEvent(self, ev):
self.emit(QtCore.SIGNAL('clicked()'))
class CustomButton(QtGui.QWidget):
def __init__(self, parent=None, *args):
super(CustomButton, self).__init__(parent)
self.setMinimumSize(300, 350)
self.setMaximumSize(300, 350)
picture = __file__.replace('qbtn.py', '') + 'mario.png'
self.button = QLabelButton(self)
self.button.setPixmap(QtGui.QPixmap(picture))
self.button.setScaledContents(True)
self.connect(self.button, QtCore.SIGNAL('clicked()'), self.onClick)
def onClick(self):
print('Button was clicked')
if __name__ == '__main__':
app = QApplication(sys.argv)
win = CustomButton()
win.show()
app.exec_()
sys.exit()
【问题讨论】:
-
注:picture = file.replace('qbtn.py', '') + 'mario.png' 指的是python文件的名字,得到一个png 文件的相对路径,位于 .py 文件的同一文件夹中。