【问题标题】:PyQt button click area (Non rectangular area)PyQt 按钮点击区域(非矩形区域)
【发布时间】: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()

mario.png

【问题讨论】:

  • 注:picture = file.replace('qbtn.py', '') + 'mario.png' 指的是python文件的名字,得到一个png 文件的相对路径,位于 .py 文件的同一文件夹中。

标签: button pyqt pyside maya


【解决方案1】:

这是我解决上述问题的最终代码......

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)

        pixmap = QtGui.QPixmap('D:\mario.png')

        self.button = QLabelButton(self)
        self.button.setPixmap(pixmap)
        self.button.setScaledContents(True)
        self.button.setMask(pixmap.mask()) # THIS DOES THE MAGIC

        self.connect(self.button, QtCore.SIGNAL('clicked()'), self.onClick)

    def onClick(self):
        print('Button was clicked')

【讨论】:

    【解决方案2】:

    您可以通过捕获按下/释放事件并使用图像中的像素值检查点击位置来决定小部件是否应该发出点击。

    class CustomButton(QWidget):
    
        def __init__(self, parent, image):
            super(CustomButton, self).__init__(parent)
            self.image = image
    
        def sizeHint(self):
            return self.image.size()
    
        def mouseReleaseEvent(self, event):
            # Position of click within the button
            pos = event.pos()
            # Assuming button is the same exact size as image
            # get the pixel value of the click point.
            pixel = self.image.alphaChannel().pixel(pos)
    
            if pixel:
                # Good click, pass the event along, will trigger a clicked signal
                super(CustomButton, self).mouseReleaseEvent(event)
            else:
                # Bad click, ignore the event, no click signal
                event.ignore()
    

    【讨论】:

    • 嗨,布伦丹,感谢您的回复。最后我得到了解决方案,只需要多一行代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    相关资源
    最近更新 更多