PyQt给QLabel添加双击事件


为了响应双击QLabel的事件,需要重写QWidget的mouseDoubleClickEvent方法。

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import *
class MyLabel(QtGui.QLabel):
    def __init__(self,parent=None):
        super(MyLabel,self).__init__(parent)
    def mouseDoubleClickEvent(self,e):
        print 'mouse double clicked'   
    def mousePressEvent(self,e):
        print 'mousePressEvent'
    def focusInEvent(self,e):
        print 'focusInEvent'
    def focusOutEvent(self,e):
        print 'focusOutEvent'
    def moveEvent(self,e):
        print 'moveEvent'
    def leaveEvent(self,e): #鼠标离开label
        print 'leaveEvent'
    def enterEvent(self,e): #鼠标移入label
        print 'enterEvent'
    def mouseMoveEvent(self,e):
        print 'mouseMoveEvent'
 
class TestDialog(QtGui.QDialog):
    def __init__(self,parent=None):
        super(TestDialog,self).__init__(parent)
        self.statusLabel = MyLabel(self)
        self.statusLabel.setGeometry(QtCore.QRect(95, 220, 151, 41))
        self.statusLabel.setText("hello label")
 
app=QApplication(sys.argv)
dialog=TestDialog()
dialog.show()


你可能感兴趣的:(python)