Extend stylesheets with custom CSS
mastercss_file argument in apply_stylesheet. You can also use placeholders like {QTMATERIAL_PRIMARYCOLOR} in your CSS to reference the current theme's colors.repository·master·Indexed 25 days ago
https://github.com/un-gcpds/qt-materialA stylesheet library for Python Qt bindings (PySide6, PySide2, PyQt5, PyQt6) that implements a Material Design aesthetic. It provides functionality to apply built-in or custom XML themes, extend styles with custom CSS, change themes at runtime via QtStyleTools, and export themes for use in C++ environments.
css_file argument in apply_stylesheet. You can also use placeholders like {QTMATERIAL_PRIMARYCOLOR} in your CSS to reference the current theme's colors.Use apply_stylesheet to apply a Material Design theme to your QApplication instance.
Important: You must import qt_material after importing your Qt backend (e.g., PySide6 or PyQt5).
For light themes, you must pass invert_secondary=True to ensure proper visibility.
import sys
from PySide6 import QtWidgets
from qt_material import apply_stylesheet
# create the application and the main window
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
# setup stylesheet
apply_stylesheet(app, theme='dark_teal.xml')
# run
window.show()
app.exec_()Install the qt-material package using pip to use Material Design stylesheets with PySide6, PySide2, PyQt5, or PyQt6.
pip install qt-materialYou can create a custom theme by generating an Android-style XML file. The file must contain specific color keys. Save this file (e.g., my_theme.xml) and pass its filename to apply_stylesheet.
<!--?xml version="1.0" encoding="UTF-8"?>
<resources>
<color name="primaryColor">#00e5ff</color>
<color name="primaryLightColor">#6effff</color>
<color name="secondaryColor">#f5f5f5</color>
<color name="secondaryLightColor">#ffffff</color>
<color name="secondaryDarkColor">#e6e6e6</color>
<color name="primaryTextColor">#000000</color>
<color name="secondaryTextColor">#000000</color>
</resources>Use export_theme to generate standalone .qss and .rcc files along with an icon folder. This allows you to use the qt-material look in non-Python environments like C++.
from qt_material import export_theme
extra = {
'danger': '#dc3545',
'warning': '#ffc107',
'success': '#17a2b8',
'font_family': 'monoespace',
'font_size': '13px',
'line_height': '13px',
'density_scale': '0',
'pyside6': True,
'linux': True,
}
export_theme(
theme='dark_teal.xml',
qss='dark_teal.qss',
rcc='resources.rcc',
output='theme',
prefix='icon:/',
invert_secondary=False,
extra=extra,
)You can adjust the widget density by passing a density_scale key inside the extra dictionary to apply_stylesheet. The default value is '0'.
extra = {
'density_scale': '-2',
}
apply_stylesheet(app, 'default', invert_secondary=False, extra=extra)Call list_themes() to see the names of all built-in XML theme files available for use with apply_stylesheet.
from qt_material import list_themes
list_themes()QMainWindow and qt_material.QtStyleTools. This provides access to apply_stylesheet (on the window instance), add_menu_theme (to add a theme selection menu), and show_dock_theme (to show a theme modification dock).The apply_stylesheet function accepts an extra dictionary to define custom accent colors for specific button classes and to set the application font family.
To use the accent colors, set the class property on the widget using setProperty('class', 'name').
extra = {
# Button colors
'danger': '#dc3545',
'warning': '#ffc107',
'success': '#17a2b8',
# Font
'font_family': 'Roboto',
}
apply_stylesheet(app, 'light_cyan.xml', invert_secondary=True, extra=extra)
# Apply the 'danger' color to a specific button
pushButton_danger.setProperty('class', 'danger')The following environment variables are available for consultation to identify the colors currently being used by the active theme:
| Environ variable | Description |
|--------------------------------|------------------------------------------|
| QTMATERIAL_PRIMARYCOLOR | Primary color |
| QTMATERIAL_PRIMARYLIGHTCOLOR | A bright version of the primary color |
| QTMATERIAL_SECONDARYCOLOR | Secondary color |
| QTMATERIAL_SECONDARYLIGHTCOLOR | A bright version of the secondary color |
| QTMATERIAL_SECONDARYDARKCOLOR | A dark version of the primary color |
| QTMATERIAL_PRIMARYTEXTCOLOR | Color for text over primary background |
| QTMATERIAL_SECONDARYTEXTCOLOR | Color for text over secondary background |
| QTMATERIAL_THEME | Name of theme used |The extra dictionary supports a QMenu key to configure specific widget parameters for menus, which is useful for handling different rendering behaviors across OS/backends.
extra['QMenu'] = {
'height': 50,
'padding': '50px 50px 50px 50px', # top, right, bottom, left
}