Created
March 24, 2024 14:32
-
-
Save ShenTengTu/51dfe446585c4368b1b179b442adea4b to your computer and use it in GitHub Desktop.
Function that generates a stylesheet for styling QComboBox & QListView (popup list)
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 import QtCore | |
from qtpy.QtWidgets import ( | |
QApplication, | |
QMainWindow, | |
QComboBox, | |
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> | |
``` | |
svg.qrc | |
``` | |
<!DOCTYPE RCC> | |
<RCC version="1.0"> | |
<qresource prefix="/"> | |
<file>svg/menu-down.svg</file> | |
</qresource> | |
</RCC> | |
``` | |
""" | |
### start of `svg.qrc` ### | |
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>\ | |
" | |
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\ | |
" | |
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\x01\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\x8ep\x96\x03\xae\ | |
" | |
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_QListView( | |
border_color: str = "#404040", | |
bg_color: str = "#fdfdfd", | |
fg_color: str = "#404040", | |
border_width: str = "1px", | |
item_selected_bg_color: str = "#ACE2E1", | |
item_selected_fg_color: str = "#008DDA", | |
): | |
return f""" | |
QListView {{ | |
background-color: {bg_color}; | |
color: {fg_color}; | |
border-width: {border_width}; | |
border-style: solid; | |
border-color: {border_color}; | |
}} | |
QListView::item:selected {{ | |
background-color: {item_selected_bg_color}; | |
color: {item_selected_fg_color}; | |
}} | |
""" | |
def style_QComboBox( | |
border_color: str = "#b0b0b0", | |
bg_color: str = "#fdfdfd", | |
fg_color: str = "#404040", | |
border_width: str = "1px", | |
border_radius: str = "4px", | |
horizontal_padding: str = "8px", | |
vertical_padding: str = "2px", | |
down_arrow_image: str = f":/svg/menu-down.svg", | |
state_styles: dict[str, dict[str, str]] = { | |
"hover": { | |
"border-color": "#404040", | |
}, | |
"on": { | |
"border-color": "#404040", | |
"border-bottom-left-radius": "0", | |
"border-bottom-right-radius": "0", | |
}, | |
"disabled": { | |
"border-color": "#b0b0b0", | |
"background-color": "#e0e0e0", | |
"color": "#808080", | |
}, | |
}, | |
drop_down_styles: dict[str, str] = {"background-color": "#e0e0e0"}, | |
): | |
qss = f""" | |
QComboBox {{ | |
border-width: {border_width}; | |
border-style: solid; | |
border-color: {border_color}; | |
border-radius: {border_radius}; | |
background-color: {bg_color}; | |
color: {fg_color}; | |
padding: {vertical_padding} {horizontal_padding}; | |
}} | |
QComboBox::down-arrow {{ | |
image: url('{down_arrow_image}'); | |
}} | |
QListView {{ | |
selection-background-color: #2020f0; | |
}} | |
""" | |
for state, styles in state_styles.items(): | |
rules = [] | |
for prop, value in styles.items(): | |
rules.append(f"{prop}: {value};") | |
if rules: | |
rules_str = "\n".join(rules) | |
qss += f""" | |
QComboBox:{state} {{ | |
{rules_str} | |
}} | |
""" | |
rules = [] | |
for prop, value in drop_down_styles.items(): | |
rules.append(f"{prop}: {value};") | |
rules_str = "\n".join(rules) | |
qss += f""" | |
QComboBox::drop-down {{ | |
border-left-width: {border_width}; | |
border-left-style: solid; | |
border-left-color: {border_color}; | |
border-top-right-radius: {border_radius}; | |
border-bottom-right-radius: {border_radius}; | |
color: {fg_color}; | |
{rules_str} | |
}} | |
""" | |
return qss | |
class Frame(QFrame): | |
def __init__(self, parent=None): | |
super().__init__(parent) | |
layout = QGridLayout() | |
layout.setSizeConstraint(QGridLayout.SizeConstraint.SetFixedSize) | |
w = QComboBox() | |
w.view().setStyleSheet(style_QListView()) | |
w.addItems(["Item1", "Item2", "Item3"]) | |
layout.addWidget(w, 0, 0) | |
w = QComboBox() | |
w.setDisabled(True) | |
w.addItem("Item1") | |
layout.addWidget(w, 0, 1) | |
self.setLayout(layout) | |
def main(): | |
app = QApplication(sys.argv) | |
app.setStyleSheet(style_QComboBox()) | |
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