2

我从 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
4

1 回答 1

4

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

要检测事件,您可以:

于 2012-05-25T21:26:39.010 回答