我想继承QLabel来添加点击事件处理。 我正在尝试这段代码:
class NewLabel(QtGui.QLabel):
def __init__(self, parent):
QtGui.QLabel.__init__(self, parent)
def clickEvent(self, event):
print 'Label clicked!'
但是点击后我没有看到“标签已点击!”行
编辑:
好吧,现在我使用的不是“clickEvent”而是“mousePressEvent”。我还有一个问题。我如何知道到底点击了哪个标签?例如,我有 2 个编辑框和 2 个标签。标签内容是像素图。所以标签中没有任何文字,所以我无法辨别标签之间的差异。我怎样才能做到这一点?
编辑2: 我编写了这段代码:
class NewLabel(QtGui.QLabel):
def __init__(self, firstLabel):
QtGui.QLabel.__init__(self, firstLabel)
def mousePressEvent(self, event):
print 'Clicked'
#myLabel = self.sender() # None =)
self.emit(QtCore.SIGNAL('clicked()'), "Label pressed")
在另一个类中:
self.FirstLang = NewLabel(Form)
QtCore.QObject.connect(self.FirstLang, QtCore.SIGNAL('clicked()'), self.labelPressed)
同一类中的插槽:
def labelPressed(self):
print 'in labelPressed'
print self.sender()
但是 self 中没有发件人对象。我做错了什么?
最佳答案
QWidget/QLabel中没有函数clickEvent
。您可以将该函数连接到 Qt 信号,或者您可以这样做:
class NewLabel(QtGui.QLabel):
def __init__(self, parent=None):
QtGui.QLabel.__init__(self, parent)
self.setText('Lorem Ipsum')
def mouseReleaseEvent(self, event):
print 'Label clicked!'
关于python - PyQt - QLabel 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/962640/