360的單個標題按鈕

#!/usr/bin/python  
#-*-coding:utf-8-*-

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.Qt import *

class ToolButton(QToolButton):
	def __init__(self,pic_name,parent = None):
		super(ToolButton,self).__init__(parent)
		
		#設置圖標
		self.pixmap= QPixmap(pic_name) 
		self.setIcon(QIcon(self.pixmap))
		self.setIconSize(self.pixmap.size())
		#設置大小
		self.setFixedSize(self.pixmap.width()+25, self.pixmap.height()+27)
		self.setAutoRaise(True)
		
		#設置文本顏色
		self.text_palette = QPalette()#palette() QPalette
		self.text_palette.setColor(self.text_palette.ButtonText, QColor(230, 230, 230))
		self.setPalette(self.text_palette)

		#設置文本粗體
		self.text_font = QFont() #QFont & const_cast<QFont &>(font())
		self.text_font.setWeight(QFont.Bold)
		
		#設置字體在下面
		self.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

		self.setStyleSheet("background:transparent")
		
		self.mouse_over  = False#鼠標是否移過
		self.mouse_press = False#鼠標是否按下
		
	def enterEvent(self,event):
		self.mouse_over = True
		self.update()
		
	
	def leaveEvent(self,event):
		self.mouse_over = False
		self.update()
		
	def mousePressEvent(self,event):
		if(event.button() == Qt.LeftButton):		
			self.clicked.emit(True)
			
	def setMousePress(self, mouse_press):
		self.mouse_press = mouse_press
		self.update()
		
	def paintEvent(self,event):
		if(self.mouse_over):
			#繪製鼠標移到按鈕上的按鈕效果
			self.painterInfo(0, 100, 150)
		else:
			if(self.mouse_press):
				self.painterInfo(0, 100, 150)
		QToolButton.paintEvent(self,event)
		
	def painterInfo(self,top_color,middle_color,bottom_color):
		self.painter = QPainter()
		self.painter.begin(self)
		self.pen = QPen(Qt.NoBrush)
		self.pen.setWidth(1)
		self.painter.setPen(self.pen)
		
		self.linear = QLinearGradient(QPointF(self.rect().topLeft()),QPointF(self.rect().bottomLeft()))
		#self.linear.start()
		self.linear.setColorAt(0, QColor(230, 230, 230, top_color))
		self.linear.setColorAt(0.5, QColor(230, 230, 230, middle_color))
		self.linear.setColorAt(1, QColor(230, 230, 230, bottom_color))
		#self.linear.finalStop()
		
		self.painter.setBrush(self.linear)
		self.painter.drawRect(self.rect()) #
		self.painter.end()

			
if __name__ == '__main__':
	
	
	import sys
	app = QApplication(sys.argv)
	tool = ToolButton("./img/toolWidget/gongNeng.png")
	#tool.setMousePress(True)
	tool.show()
	sys.exit(app.exec_())

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章