代码如下:
#!/usr/bin/python #-*-coding:utf-8-*-
from pyqt4.qtgui import *from pyqt4.qtcore import *from pyqt4.qt import *
class clabel(qwidget): signallabelpress = pyqtsignal() def __init__(self,parent = none): super(clabel,self).__init__(parent) self.initvariable() self.initsetupui() #self.m_mouseenterflag = false #self.m_mousepressflag = true #pointer members #self.m_plabelicon = qlabel(self) #self.m_plabeltext = qlabel(self) def setpixmap(self,pixmap): self.m_plabelicon.setpixmap(pixmap.scaled(qsize(30, 30), qt.keepaspectratio, qt.smoothtransformation))
def settext(self,text): self.m_plabeltext.settext(text)
def setmouseenterflag(self, flag): self.m_mouseenterflag = flag self.update()
def setmousepressflag(self, flag): self.m_mousepressflag = flag self.update()
def enterevent(self,event): if(~self.getmousepressflag()): self.setmouseenterflag(true) self.setcursor(qt.pointinghandcursor)
def leaveevent(self,event): self.setmouseenterflag(false)
def mousepressevent(self,e): if(e.button() == qt.leftbutton): self.setmousepressflag(true) self.signallabelpress.emit()
def paintevent(self,e): painter = qpainter(self) if(self.getmouseenterflag()): self.paintwidget(50, painter) elif(self.getmousepressflag()): self.paintwidget(80, painter) qwidget.paintevent(self,e)
def initvariable(self): self.setmouseenterflag(false) self.setmousepressflag(false)
def initsetupui(self): self.createframe() self.createwidget() self.createlayout()
def createframe(self): self.setstylesheet(“qwidget background:transparentborder:0pxcolor:whitefont-weight:boldfont-size:16px”)
def createwidget(self): self.m_plabelicon = qlabel(self) self.m_plabeltext = qlabel(self)
def createlayout(self): self.m_phlayout = qhboxlayout() self.m_phlayout.setspacing(10) self.m_phlayout.setcontentsmargins(qmargins(5, 0, 5, 0)) self.m_phlayout.addwidget(self.m_plabelicon) self.m_phlayout.addwidget(self.m_plabeltext) self.m_phlayout.addstretch()
self.setlayout(self.m_phlayout)
def paintwidget(self,transparency,device): #qpainter * #self.pen = qpen(qt.nobrush) #self.pen.setwidth(1) device.setpen(qt.nopen) self.linear = qlineargradient(qpointf(self.rect().topleft()), qpointf(self.rect().bottomleft())) self.linear.setcolorat(0, qcolor(255, 255, 255, transparency)) self.brush = qbrush(self.linear) device.setbrush(self.brush) device.drawroundedrect(self.rect(), 2, 2)
def getmouseenterflag(self): return self.m_mouseenterflag
def getmousepressflag(self): return self.m_mousepressflag
if __name__ == ‘__main__’: import sys app = qapplication(sys.argv) label = clabel() label.show() sys.exit(app.exec_())