Created
March 26, 2024 06:47
-
-
Save ShenTengTu/48d545c70c5631995ebc490b4b5bbab3 to your computer and use it in GitHub Desktop.
Function that generates a stylesheet for styling QCheckBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from qtpy.QtWidgets import ( | |
QApplication, | |
QMainWindow, | |
QCheckBox, | |
QFrame, | |
QGridLayout, | |
) | |
""" | |
check.svg | |
``` | |
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>check</title><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z" /></svg> | |
``` | |
svg.qrc | |
``` | |
<!DOCTYPE RCC> | |
<RCC version="1.0"> | |
<qresource prefix="/"> | |
<file>svg/check.svg</file> | |
</qresource> | |
</RCC> | |
``` | |
""" | |
### start of `svg.qrc` ### | |
from PySide6 import QtCore | |
qt_resource_data = b"\ | |
\x00\x00\x00p\ | |
<\ | |
svg xmlns=\x22http:\ | |
//www.w3.org/200\ | |
0/svg\x22 viewBox=\x22\ | |
0 0 24 24\x22><titl\ | |
e>square</title>\ | |
<path d=\x22M3,3V21\ | |
H21V3\x22 /></svg>\ | |
\x00\x00\x00\x9a\ | |
<\ | |
svg xmlns=\x22http:\ | |
//www.w3.org/200\ | |
0/svg\x22 viewBox=\x22\ | |
0 0 24 24\x22><titl\ | |
e>check</title><\ | |
path d=\x22M21,7L9,\ | |
19L3.5,13.5L4.91\ | |
,12.09L9,16.17L1\ | |
9.59,5.59L21,7Z\x22\ | |
/></svg>\ | |
" | |
qt_resource_name = b"\ | |
\x00\x03\ | |
\x00\x00z\xc7\ | |
\x00s\ | |
\x00v\x00g\ | |
\x00\x0a\ | |
\x08\x8b\x0b\xa7\ | |
\x00s\ | |
\x00q\x00u\x00a\x00r\x00e\x00.\x00s\x00v\x00g\ | |
\x00\x09\ | |
\x0b\x9e\x89\x07\ | |
\x00c\ | |
\x00h\x00e\x00c\x00k\x00.\x00s\x00v\x00g\ | |
" | |
qt_resource_struct = b"\ | |
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\ | |
\x00\x00\x00\x00\x00\x00\x00\x00\ | |
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\x02\ | |
\x00\x00\x00\x00\x00\x00\x00\x00\ | |
\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\ | |
\x00\x00\x01\x8eyp]\xe3\ | |
\x00\x00\x00&\x00\x00\x00\x00\x00\x01\x00\x00\x00t\ | |
\x00\x00\x01\x8ey`N\xad\ | |
" | |
def qInitResources(): | |
QtCore.qRegisterResourceData( | |
0x03, qt_resource_struct, qt_resource_name, qt_resource_data | |
) | |
def qCleanupResources(): | |
QtCore.qUnregisterResourceData( | |
0x03, qt_resource_struct, qt_resource_name, qt_resource_data | |
) | |
qInitResources() | |
### end of `svg.qrc` ### | |
def style_QCheckBox( | |
border_width: str = "1px", | |
border_radius: str = "4px", | |
size: str = "13px", | |
padding: str = "1px", | |
spacing: str = "2px", | |
checked_image=":/svg/check.svg", | |
indeterminate_image=":/svg/square.svg", | |
border_color: str = "#b0b0b0", | |
background_color: str = "#fdfdfd", | |
color: str = "#404040", | |
hover_border_color: str = "#404040", | |
disabled_border_color: str = "#b0b0b0", | |
disabled_background_color: str = "#e0e0e0", | |
disabled_color: str = "#808080", | |
): | |
qss = f""" | |
QCheckBox {{ | |
spacing: {spacing}; | |
}} | |
QCheckBox::indicator {{ | |
width: {size}; | |
height: {size}; | |
border-width: {border_width}; | |
border-style: solid; | |
border-color: {border_color}; | |
border-radius: {border_radius}; | |
background-color: {background_color}; | |
color: {color}; | |
padding: {padding}; | |
}} | |
QCheckBox::indicator:checked {{ | |
image: url('{checked_image}'); | |
}} | |
QCheckBox::indicator:indeterminate {{ | |
image: url('{indeterminate_image}'); | |
}} | |
QCheckBox::indicator:hover {{ | |
border-color: {hover_border_color}; | |
}} | |
QCheckBox::indicator:disabled {{ | |
border-color: {disabled_border_color}; | |
background-color: {disabled_background_color}; | |
color: {disabled_color}; | |
}} | |
""" | |
return qss | |
class Frame(QFrame): | |
def __init__(self, parent=None): | |
super().__init__(parent) | |
layout = QGridLayout() | |
layout.setSizeConstraint(QGridLayout.SizeConstraint.SetFixedSize) | |
w = QCheckBox() | |
layout.addWidget(w, 0, 0) | |
w = QCheckBox() | |
w.setChecked(True) | |
layout.addWidget(w, 0, 1) | |
w = QCheckBox() | |
w.setCheckState(QtCore.Qt.CheckState.PartiallyChecked) | |
layout.addWidget(w, 0, 2) | |
w = QCheckBox() | |
w.setDisabled(True) | |
layout.addWidget(w, 1, 0) | |
w = QCheckBox() | |
w.setChecked(True) | |
w.setDisabled(True) | |
layout.addWidget(w, 1, 1) | |
w = QCheckBox() | |
w.setCheckState(QtCore.Qt.CheckState.PartiallyChecked) | |
w.setDisabled(True) | |
layout.addWidget(w, 1, 2) | |
self.setLayout(layout) | |
def main(): | |
app = QApplication(sys.argv) | |
app.setStyleSheet(style_QCheckBox()) | |
w = QMainWindow() | |
w.setMinimumSize(200, 100) | |
w.setCentralWidget(Frame(w)) | |
w.show() | |
sys.exit(app.exec()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment