PyQt圆形按钮

不规则形状的控件一直是图形界面设计的不可缺少的一项,但是有些控件的不规则实现却比较困难。这篇文字专注于圆形按钮的实现,此实现方法比较简单,没有任何难点,且看实现效果。


按钮初始图片:


Hovered图片:


Pressed图片:


当然,将图片设置为rgb色值也可以,只需要稍稍修改即可。


圆形按钮实现的关键代码只有一句:

painter_path.addEllipse(1,1, button_rect.width()-2, button_rect.height()-2)

painter.setClipPath(painter_path)

稍微百度或者其他搜索一下,即可知道addEllipse方法是得到一个椭圆,而圆是椭圆的一个特殊例子,具体实现方法,请仔细参阅源代码。


本实例的运行效果如下图:


主要按钮实现源码如下:

#coding:utf-8
'''
Created on 2014年8月26日
@author : GuoWu
'''
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class AeroButton(QPushButton):
    def __init__(self,a,b,c,parent=None):
        super(AeroButton,self).__init__(parent)
        #self.setEnabled(True)
        self.a = a
        self.b = b
        self.c = c
        self.hovered = False
        self.pressed = False
        self.color = QColor(Qt.gray)
        self.hightlight = QColor(Qt.lightGray)
        self.shadow = QColor(Qt.black)
        self.opacity = 1.0
        self.roundness = 0
        
    def paintEvent(self,event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        if (self.isEnabled()):
            if self.hovered:
                self.color=self.hightlight.darker(250)
        else:
            self.color = QColor(50,50,50)
            
        button_rect = QRect(self.geometry())
        painter.setPen(QPen(QBrush(Qt.red),2.0))
        painter_path = QPainterPath()
        #painter_path.addRoundedRect(1, 1, button_rect.width() - 2, button_rect.height() - 2, self.roundness, self.roundness)
        painter_path.addEllipse(1, 1, button_rect.width()-2, button_rect.height()-2)
        painter.setClipPath(painter_path)
        if self.isEnabled():
            if (self.pressed == False and self.hovered == False):
                icon_size = self.iconSize()
                icon_position = self.calculateIconPosition(button_rect,icon_size)
                painter.setOpacity(1.0)
                painter.drawPixmap(icon_position, QPixmap(QIcon(self.a).pixmap(icon_size)))
            elif (self.hovered == True and self.pressed == False):
                icon_size = self.iconSize()
                icon_position = self.calculateIconPosition(button_rect,icon_size)
                painter.setOpacity(1.0)
                painter.drawPixmap(icon_position, QPixmap(QIcon(self.b).pixmap(icon_size)))
            elif self.pressed == True:   
                icon_size = self.iconSize()
                icon_position = self.calculateIconPosition(button_rect,icon_size)
                painter.setOpacity(1.0)
                painter.drawPixmap(icon_position, QPixmap(QIcon(self.c).pixmap(icon_size)))
        else:
            icon_size = self.iconSize()
            icon_position = self.calculateIconPosition(button_rect,icon_size)
            painter.setOpacity(1.0)
            painter.drawPixmap(icon_position, QPixmap(QIcon(self.a).pixmap(icon_size)))
    def enterEvent(self,event):
        self.hovered = True
        self.repaint()
        QPushButton.enterEvent(self,event)
    def leaveEvent(self,event):
        self.hovered = False;
        self.repaint()
        QPushButton.leaveEvent(self,event)
    def mousePressEvent(self,event):
        self.pressed = True
        self.repaint()
        QPushButton.mousePressEvent(self,event)
    def mouseReleaseEvent(self,event):

        self.pressed = False
        self.repaint()
        QPushButton.mouseReleaseEvent(self,event)
    def calculateIconPosition(self,button_rect,icon_size):

        x = (button_rect.width() / 2) - (icon_size.width() / 2)
        y = (button_rect.height() / 2) - (icon_size.height() / 2)

        width = icon_size.width()
        height = icon_size.height()

        icon_position = QRect()
        icon_position.setX(x)
        icon_position.setY(y)
        icon_position.setWidth(width)
        icon_position.setHeight(height)

        return icon_position
        

本文参考某个Qt的圆形按钮实现程序,对其表示感谢。

技术略低,不喜勿喷。

邮箱528081202@qq.com,求交流指教。

转载于:https://my.oschina.net/u/2406006/blog/714263

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值