- 首先看一下实现的基本效果如下所示:
- 实现的源码如下:
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from widgets.components.ui_types import ButtonType, ButtonSize, BorderStyle
from widgets.components.styles.button_style import ButtonStyle
class BaseCheckBox(QCheckBox):
"""
基础的StyleSheet
"""
def __init__(self, *args):
super(BaseCheckBox, self).__init__(*args)
self.__init_style() # 设置样式
def __init_style(self):
checkbox_style = '''
QCheckBox {
border: none;
border-radius: 13px;
}
QCheckBox::indicator{
background-color: rgb(42, 45, 66);
border: 0px solid #b1b1b1;
width: 46px;
height: 26px;
border-radius: 13px;
}
QCheckBox:enabled:checked{
color: rgb(255, 255, 255);
}
QCheckBox:enabled:!checked{
color: rgb(255, 255, 255);
}
QCheckBox::indicator:checked {
background-color: rgb(27, 177, 193);
}
QCheckBox::indicator:unchecked {
background-color: rgb(42, 45, 66);
}
'''
self.setStyleSheet(self.styleSheet() + checkbox_style)
def mousePressEvent(self, *args, **kwargs):
return super(BaseCheckBox, self).mousePressEvent(*args, **kwargs)
def mouseReleaseEvent(self, *args, **kwargs):
return super(BaseCheckBox, self).mouseReleaseEvent(*args, **kwargs)
def paintEvent(self, pa : QPaintEvent):
super(BaseCheckBox, self).paintEvent(pa)
if self.isChecked():
painter = QPainter(self)
painter.setPen(Qt.white)
painter.setBrush(Qt.white)
# painter.drawText(self.rect(), Qt.AlignLeft, "test")
painter.drawEllipse(24, 4, 18, 18)
else:
painter = QPainter(self)
painter.setPen(Qt.white)
painter.setBrush(Qt.white)
# painter.drawText(self.rect(), Qt.AlignLeft, "test")
painter.drawEllipse(5, 4, 18, 18)
- 实现的思路简单介绍一下:
- 主要是使用QPainter来绘制圆形框,来实现选中的特效,加上背景色的调整,实现选中和非选中,无需下载任何的资源
- 简单的实现了一下,hover等状态还没实现,但是思路是一样的,借鉴一下思路就ok了
- 使用的话,只需要是按照
QCheckBox
的基础用法来使用即可