class - pyqt-如何为 QLabel 类创建点击信号?

标签 class button pyqt signals qlabel

我已经从 QLabel 创建了一个图像按钮类。现在我想添加一个点击信号。现在,该怎么办?

 class PicButton(QLabel):

    def __init__(self,pixmap1,pixmap2,parent=None):
        QLabel.__init__(self, parent) 
        self.pic = pixmap1
        imageSize = self.pic.size()
        imageSize.setWidth(imageSize.width()*.7)
        imageSize.setHeight(imageSize.height()*.7)
        self.pic = self.pic.scaled(imageSize,Qt.KeepAspectRatioByExpanding)



        self.pic2 = pixmap2
        imageSize2 = self.pic2.size()
        imageSize2.setWidth(imageSize2.width()*.7)
        imageSize2.setHeight(imageSize2.height()*.7)
        self.pic2 = self.pic2.scaled(imageSize2,Qt.KeepAspectRatioByExpanding)



    def enterEvent(self, event):
        self.setPixmap(self.pic2)
        event.accept()

    def leaveEvent(self, event):
        """ When the mouse leave this widget, destroy it. """
        self.setPixmap(self.pic)
        self.destroy()

我做了一个按钮:

  ImageButton2 = PicButton(QPixmap("image/buy.png"),QPixmap("image/buy_Hover.png"))
  ImageButton2.clicked.connect(self.addStuff)  # but this class has not  clicked signal

最佳答案

QLabels 没有默认的clicked 信号。如果你想拥有一个,你必须add it你自己并在正确的事件上发出它。

要检测该事件,您可以:

关于class - pyqt-如何为 QLabel 类创建点击信号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10757077/

相关文章:

javascript - 是不是可以在 JS CLASS 中使用 dom 修改?

objective-c - 在 iOS 中以编程方式实例化类

c++ - 在主函数 c++ 中使用类时出现两个错误

python - Scipy.Stats.exponweib 的子类不调用 __init__ 或其他函数

java - 如何在 java 中有效地使用 cardlayout 以便使用各种面板构造函数中的按钮从面板切换

html - 在 <button> 元素中使用图像

c# - Unity Array of Struct : When setting a variable of one of the array subscripts, 它为所有这些设置它

python - 更新 Qt 中的子部件

html - PySide QTextEdit html 表格宽度

python - 将 QTableWidget 写入 .csv 或 .xls