QT Material Readthedocs Io en Latest
QT Material Readthedocs Io en Latest
Yeison Cardona
1 Navigation 3
2 Install 5
3 Usage 7
4 Themes 9
5 Custom colors 11
6 Light themes 13
7 Environ variables 15
9 Custom stylesheets 19
10 Run examples 21
11 New themes 23
14 Export theme 29
15 Density scale 31
16 Troubleshoots 33
16.1 QMenu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
17 Navigation 35
i
ii
Qt Material Documentation
This is another stylesheet for PySide6, PySide2, PyQt5 and PyQt6, which looks like Material Design (close enough).
CONTENTS 1
Qt Material Documentation
2 CONTENTS
CHAPTER
ONE
NAVIGATION
• Install
• Usage
• Themes
• Custom colors
• Usage
• Light themes
• Environ variables
• Alternative QPushButtons and custom fonts
• Custom stylesheets
• Run examples
• New themes
• Change theme in runtime
• Export theme
• Density scale
• Troubleshoots
3
Qt Material Documentation
4 Chapter 1. Navigation
CHAPTER
TWO
INSTALL
5
Qt Material Documentation
6 Chapter 2. Install
CHAPTER
THREE
USAGE
import sys
from PySide6 import QtWidgets
# from PySide2 import QtWidgets
# from PyQt5 import QtWidgets
from qt_material import apply_stylesheet
# setup stylesheet
apply_stylesheet(app, theme='dark_teal.xml')
# run
window.show()
app.exec_()
7
Qt Material Documentation
8 Chapter 3. Usage
CHAPTER
FOUR
THEMES
list_themes()
['dark_amber.xml',
'dark_blue.xml',
'dark_cyan.xml',
'dark_lightgreen.xml',
'dark_pink.xml',
'dark_purple.xml',
'dark_red.xml',
'dark_teal.xml',
'dark_yellow.xml',
'light_amber.xml',
'light_blue.xml',
'light_cyan.xml',
'light_cyan_500.xml',
'light_lightgreen.xml',
'light_pink.xml',
'light_purple.xml',
'light_red.xml',
'light_teal.xml',
'light_yellow.xml']
9
Qt Material Documentation
10 Chapter 4. Themes
CHAPTER
FIVE
CUSTOM COLORS
Color Tool is the best way to generate new themes, just choose colors and export as Android XML, the theme file must
look like:
Save it as my_theme.xml or similar and apply the style sheet from Python.
apply_stylesheet(app, theme='dark_teal.xml')
11
Qt Material Documentation
SIX
LIGHT THEMES
13
Qt Material Documentation
SEVEN
ENVIRON VARIABLES
There is a environ variables related to the current theme used, these variables are for consult purpose only.
15
Qt Material Documentation
EIGHT
extra = {
# Button colors
'danger': '#dc3545',
'warning': '#ffc107',
'success': '#17a2b8',
# Font
'font_family': 'Roboto',
}
The accent colors are applied to QPushButton with the corresponding class property:
pushButton_danger.setProperty('class', 'danger')
pushButton_warning.setProperty('class', 'warning')
pushButton_success.setProperty('class', 'success')
_images/extra.png
Fig. 1: extra
17
Qt Material Documentation
NINE
CUSTOM STYLESHEETS
QPushButton {{
color: {QTMATERIAL_SECONDARYCOLOR};
text-transform: none;
background-color: {QTMATERIAL_PRIMARYCOLOR};
}}
.big_button {{
height: 64px;
}}
stylesheet = app.styleSheet()
with open('custom.css') as file:
app.setStyleSheet(stylesheet + file.read().format(**os.environ))
And the class style can be applied with the setProperty method:
self.main.pushButton.setProperty('class', 'big_button')
_images/custom.png
Fig. 1: extra
19
Qt Material Documentation
TEN
RUN EXAMPLES
A window with almost all widgets (see the previous screenshots) are available to test all themes and create new ones.
Fig. 1: theme
21
Qt Material Documentation
ELEVEN
NEW THEMES
Do you have a custom theme? it looks good? create a pull request in themes folder and share it with all users.
23
Qt Material Documentation
TWELVE
There is a qt_material.QtStyleTools class that must be inherited along to QMainWindow for change themes in
runtime using the apply_stylesheet() method.
def __init__(self):
super().__init__()
self.main = QUiLoader().load('main_window.ui', self)
self.apply_stylesheet(self.main, 'dark_teal.xml')
# self.apply_stylesheet(self.main, 'light_red.xml')
# self.apply_stylesheet(self.main, 'light_blue.xml')
Fig. 1: run
A custom stylesheets menu can be added to a project for switching across all default available themes.
def __init__(self):
super().__init__()
self.main = QUiLoader().load('main_window.ui', self)
self.add_menu_theme(self.main, self.main.menuStyles)
Fig. 2: menu
25
Qt Material Documentation
THIRTEEN
A simple interface is available to modify a theme in runtime, this feature can be used to create a new theme, the theme
file is created in the main directory as my_theme.xml
def __init__(self):
super().__init__()
self.main = QUiLoader().load('main_window.ui', self)
self.show_dock_theme(self.main)
Fig. 1: dock
27
Qt Material Documentation
FOURTEEN
EXPORT THEME
This feature able to use qt-material themes into Qt implementations using only local files.
extra = {
# Button colors
'danger': '#dc3545',
'warning': '#ffc107',
'success': '#17a2b8',
# Font
'font_family': 'monoespace',
'font_size': '13px',
'line_height': '13px',
# Density Scale
'density_scale': '0',
# environ
'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,
)
This script will generate both dark_teal.qss and resources.rcc and a folder with all theme icons called theme.
The files generated can be integrated into a PySide6 application just with:
import sys
29
Qt Material Documentation
# Create application
app = QtWidgets.QApplication(sys.argv)
# Load styles
with open('dark_teal.qss', 'r') as file:
app.style_sheet = file.read()
# Load icons
QDir.add_search_path('icon', 'theme')
# App
window = QtWidgets.QMainWindow()
checkbox = QtWidgets.QCheckBox(window)
checkbox.text = 'CheckBox'
window.show()
app.exec()
This files can also be used into non Python environs like C++.
FIFTEEN
DENSITY SCALE
The extra arguments also include an option to set the density scale, by default is 0.
extra = {
# Density Scale
'density_scale': '-2',
}
Fig. 1: dock
31
Qt Material Documentation
SIXTEEN
TROUBLESHOOTS
16.1 QMenu
QMenu has multiple rendering for each Qt backend, and for each operating system. Even can be related with the style, like
fusion. Then, the extra argument also supportsQMenu parameters to configure this widgest for specific combinations.
This options are not affected by density scale.
extra['QMenu'] = {
'height': 50,
'padding': '50px 50px 50px 50px', # top, right, bottom, left
}
33
Qt Material Documentation
SEVENTEEN
NAVIGATION
35
Qt Material Documentation
EIGHTEEN
• genindex
• modindex
• search
37