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()


发布了47 篇原创文章 · 获赞 17 · 访问量 34万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览