Created
March 26, 2024 05:52
-
-
Save ShenTengTu/1c0ec10ca969aefd19e1d9502c076870 to your computer and use it in GitHub Desktop.
Function that generates a stylesheet for styling QSpinBox & QDoubleSpinBox
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, | |
QSpinBox, | |
QDoubleSpinBox, | |
QFrame, | |
QGridLayout, | |
) | |
""" | |
menu-down.svg | |
``` | |
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>menu-down</title><path d="M7,10L12,15L17,10H7Z" /></svg> | |
``` | |
menu-up.svg | |
``` | |
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>menu-up</title><path d="M7,15L12,10L17,15H7Z" /></svg> | |
``` | |
svg.qrc | |
``` | |
<!DOCTYPE RCC> | |
<RCC version="1.0"> | |
<qresource prefix="/"> | |
<file>svg/menu-down.svg</file> | |
<file>svg/menu-up.svg</file> | |
</qresource> | |
</RCC> | |
``` | |
""" | |
### start of `svg.qrc` ### | |
from PySide6 import QtCore | |
qt_resource_data = b"\ | |
\x00\x00\x00{\ | |
<\ | |
svg xmlns=\x22http:\ | |
//www.w3.org/200\ | |
0/svg\x22 viewBox=\x22\ | |
0 0 24 24\x22><titl\ | |
e>menu-down</tit\ | |
le><path d=\x22M7,1\ | |
0L12,15L17,10H7Z\ | |
\x22 /></svg>\ | |
\x00\x00\x00y\ | |
<\ | |
svg xmlns=\x22http:\ | |
//www.w3.org/200\ | |
0/svg\x22 viewBox=\x22\ | |
0 0 24 24\x22><titl\ | |
e>menu-up</title\ | |
><path d=\x22M7,15L\ | |
12,10L17,15H7Z\x22 \ | |
/></svg>\ | |
" | |
qt_resource_name = b"\ | |
\x00\x03\ | |
\x00\x00z\xc7\ | |
\x00s\ | |
\x00v\x00g\ | |
\x00\x0d\ | |
\x01j\x5cg\ | |
\x00m\ | |
\x00e\x00n\x00u\x00-\x00d\x00o\x00w\x00n\x00.\x00s\x00v\x00g\ | |
\x00\x0b\ | |
\x04$\xd1\xc7\ | |
\x00m\ | |
\x00e\x00n\x00u\x00-\x00u\x00p\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\x8et\xfe/\xbb\ | |
\x00\x00\x00,\x00\x00\x00\x00\x00\x01\x00\x00\x00\x7f\ | |
\x00\x00\x01\x8ey?+\xf7\ | |
" | |
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_QSpinBox( | |
border_width: str = "1px", | |
border_radius: str = "4px", | |
horizontal_padding="8px", | |
vertical_padding: str = "2px", | |
up_arrow_image=":/svg/menu-up.svg", | |
down_arrow_image=":/svg/menu-down.svg", | |
border_color: str = "#b0b0b0", | |
background_color: str = "#fdfdfd", | |
color: str = "#404040", | |
placeholder_color: str = "#808080", | |
selection_background_color: str = "#404040", | |
selection_color: str = "#fdfdfd", | |
hover_border_color: str = "#404040", | |
disabled_border_color: str = "#b0b0b0", | |
disabled_background_color: str = "#e0e0e0", | |
disabled_color: str = "#808080", | |
): | |
qss = f""" | |
QSpinBox, QDoubleSpinBox {{ | |
border-width: {border_width}; | |
border-style: solid; | |
border-color: {border_color}; | |
border-radius: {border_radius}; | |
background-color: {background_color}; | |
color: {color}; | |
padding: {vertical_padding} {horizontal_padding}; | |
placeholder-text-color: {placeholder_color}; | |
selection-background-color: {selection_background_color}; | |
selection-color: {selection_color}; | |
}} | |
QSpinBox::hover, QDoubleSpinBox::hover {{ | |
border-color: {hover_border_color}; | |
}} | |
QSpinBox::disabled, QDoubleSpinBox::disabled {{ | |
border-color: {disabled_border_color}; | |
background-color: {disabled_background_color}; | |
color: {disabled_color}; | |
}} | |
QSpinBox::up-arrow, QDoubleSpinBox::up-arrow {{ | |
image: url('{up_arrow_image}'); | |
}} | |
QSpinBox::down-arrow, QDoubleSpinBox::down-arrow {{ | |
image: url('{down_arrow_image}'); | |
}} | |
QSpinBox::up-button, QDoubleSpinBox::up-button, | |
QSpinBox::down-button, QDoubleSpinBox::down-button {{ | |
border-style: solid; | |
border-left-width: {border_width}; | |
border-left-color: {border_color}; | |
background-color: {background_color}; | |
color: {color}; | |
}} | |
QSpinBox::up-button:hover, QDoubleSpinBox::up-button:hover, | |
QSpinBox::down-button:hover, QDoubleSpinBox::down-button:hover {{ | |
border-color: {hover_border_color}; | |
}} | |
QSpinBox::up-button, QDoubleSpinBox::up-button {{ | |
border-top-right-radius: {border_radius}; | |
}} | |
QSpinBox::down-button, QDoubleSpinBox::down-button {{ | |
border-bottom-right-radius: {border_radius}; | |
}} | |
""" | |
return qss | |
class Frame(QFrame): | |
def __init__(self, parent=None): | |
super().__init__(parent) | |
layout = QGridLayout() | |
layout.setSizeConstraint(QGridLayout.SizeConstraint.SetFixedSize) | |
w = QSpinBox() | |
layout.addWidget(w, 0, 0) | |
w = QSpinBox() | |
w.setDisabled(True) | |
layout.addWidget(w, 0, 1) | |
w = QDoubleSpinBox() | |
layout.addWidget(w, 1, 0) | |
w = QDoubleSpinBox() | |
w.setDisabled(True) | |
layout.addWidget(w, 1, 1) | |
self.setLayout(layout) | |
def main(): | |
app = QApplication(sys.argv) | |
app.setStyleSheet(style_QSpinBox()) | |
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