| 1 |
Qt-Material |
| 2 |
=========== |
| 3 |
|
| 4 |
This is another stylesheet for **PySide6**, **PySide2**, **PyQt5** and |
| 5 |
**PyQt6**, which looks like Material Design (close enough). |
| 6 |
|
| 7 |
|GitHub top language| |PyPI - License| |PyPI| |PyPI - Status| |PyPI - |
| 8 |
Python Version| |GitHub last commit| |CodeFactor Grade| |Documentation |
| 9 |
Status| |
| 10 |
|
| 11 |
.. |GitHub top language| image:: https://img.shields.io/github/languages/top/un-gcpds/qt-material |
| 12 |
.. |PyPI - License| image:: https://img.shields.io/pypi/l/qt-material |
| 13 |
.. |PyPI| image:: https://img.shields.io/pypi/v/qt-material |
| 14 |
.. |PyPI - Status| image:: https://img.shields.io/pypi/status/qt-material |
| 15 |
.. |PyPI - Python Version| image:: https://img.shields.io/pypi/pyversions/qt-material |
| 16 |
.. |GitHub last commit| image:: https://img.shields.io/github/last-commit/un-gcpds/qt-material |
| 17 |
.. |CodeFactor Grade| image:: https://img.shields.io/codefactor/grade/github/UN-GCPDS/qt-material |
| 18 |
.. |Documentation Status| image:: https://readthedocs.org/projects/qt-material/badge/?version=latest |
| 19 |
:target: https://qt-material.readthedocs.io/en/latest/?badge=latest |
| 20 |
|
| 21 |
There is some custom dark themes: |dark| And light: |light| |
| 22 |
|
| 23 |
.. |dark| image:: _images/dark.gif |
| 24 |
.. |light| image:: _images/light.gif |
| 25 |
|
| 26 |
Navigation |
| 27 |
---------- |
| 28 |
|
| 29 |
- `Install <#install>`__ |
| 30 |
- `Usage <#usage>`__ |
| 31 |
- `Themes <#themes>`__ |
| 32 |
- `Custom colors <#custom-colors>`__ |
| 33 |
- `Usage <#usage>`__ |
| 34 |
- `Light themes <#light-themes>`__ |
| 35 |
- `Environ variables <#environ-variables>`__ |
| 36 |
- `Alternative QPushButtons and custom |
| 37 |
fonts <#alternative-qpushbuttons-and-custom-fonts>`__ |
| 38 |
- `Custom stylesheets <#custom-stylesheets>`__ |
| 39 |
- `Run examples <#run-examples>`__ |
| 40 |
- `New themes <#new-themes>`__ |
| 41 |
- `Change theme in runtime <#change-theme-in-runtime>`__ |
| 42 |
- `Export theme <#export-theme>`__ |
| 43 |
- `Density scale <#density-scale>`__ |
| 44 |
- `Troubleshoots <#troubleshoots>`__ |
| 45 |
|
| 46 |
Install |
| 47 |
------- |
| 48 |
|
| 49 |
.. code:: ipython3 |
| 50 |
|
| 51 |
pip install qt-material |
| 52 |
|
| 53 |
Usage |
| 54 |
----- |
| 55 |
|
| 56 |
.. code:: ipython3 |
| 57 |
|
| 58 |
import sys |
| 59 |
from PySide6 import QtWidgets |
| 60 |
# from PySide2 import QtWidgets |
| 61 |
# from PyQt5 import QtWidgets |
| 62 |
from qt_material import apply_stylesheet |
| 63 |
|
| 64 |
# create the application and the main window |
| 65 |
app = QtWidgets.QApplication(sys.argv) |
| 66 |
window = QtWidgets.QMainWindow() |
| 67 |
|
| 68 |
# setup stylesheet |
| 69 |
apply_stylesheet(app, theme='dark_teal.xml') |
| 70 |
|
| 71 |
# run |
| 72 |
window.show() |
| 73 |
app.exec_() |
| 74 |
|
| 75 |
Themes |
| 76 |
------ |
| 77 |
|
| 78 |
.. code:: ipython3 |
| 79 |
|
| 80 |
from qt_material import list_themes |
| 81 |
|
| 82 |
list_themes() |
| 83 |
|
| 84 |
|
| 85 |
.. parsed-literal:: |
| 86 |
|
| 87 |
WARNING:root:qt_material must be imported after PySide or PyQt! |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
.. parsed-literal:: |
| 93 |
|
| 94 |
['dark_amber.xml', |
| 95 |
'dark_blue.xml', |
| 96 |
'dark_cyan.xml', |
| 97 |
'dark_lightgreen.xml', |
| 98 |
'dark_pink.xml', |
| 99 |
'dark_purple.xml', |
| 100 |
'dark_red.xml', |
| 101 |
'dark_teal.xml', |
| 102 |
'dark_yellow.xml', |
| 103 |
'light_amber.xml', |
| 104 |
'light_blue.xml', |
| 105 |
'light_cyan.xml', |
| 106 |
'light_cyan_500.xml', |
| 107 |
'light_lightgreen.xml', |
| 108 |
'light_pink.xml', |
| 109 |
'light_purple.xml', |
| 110 |
'light_red.xml', |
| 111 |
'light_teal.xml', |
| 112 |
'light_yellow.xml'] |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
Custom colors |
| 117 |
------------- |
| 118 |
|
| 119 |
`Color Tool <https://material.io/resources/color/>`__ is the best way to |
| 120 |
generate new themes, just choose colors and export as ``Android XML``, |
| 121 |
the theme file must look like: |
| 122 |
|
| 123 |
.. code:: ipython3 |
| 124 |
|
| 125 |
<!--?xml version="1.0" encoding="UTF-8"?--> |
| 126 |
<resources> |
| 127 |
<color name="primaryColor">#00e5ff</color> |
| 128 |
<color name="primaryLightColor">#6effff</color> |
| 129 |
<color name="secondaryColor">#f5f5f5</color> |
| 130 |
<color name="secondaryLightColor">#ffffff</color> |
| 131 |
<color name="secondaryDarkColor">#e6e6e6</color> |
| 132 |
<color name="primaryTextColor">#000000</color> |
| 133 |
<color name="secondaryTextColor">#000000</color> |
| 134 |
</resources> |
| 135 |
|
| 136 |
Save it as ``my_theme.xml`` or similar and apply the style sheet from |
| 137 |
Python. |
| 138 |
|
| 139 |
.. code:: ipython3 |
| 140 |
|
| 141 |
apply_stylesheet(app, theme='dark_teal.xml') |
| 142 |
|
| 143 |
Light themes |
| 144 |
------------ |
| 145 |
|
| 146 |
Light themes will need to add ``invert_secondary`` argument as ``True``. |
| 147 |
|
| 148 |
.. code:: ipython3 |
| 149 |
|
| 150 |
apply_stylesheet(app, theme='light_red.xml', invert_secondary=True) |
| 151 |
|
| 152 |
Environ variables |
| 153 |
----------------- |
| 154 |
|
| 155 |
There is a environ variables related to the current theme used, these |
| 156 |
variables are for **consult purpose only**. |
| 157 |
|
| 158 |
+------------------------+--------------------------------+-----------+ |
| 159 |
| Environ variable | Description | Example | |
| 160 |
+========================+================================+===========+ |
| 161 |
| Q | Primary color | #2979ff | |
| 162 |
| TMATERIAL_PRIMARYCOLOR | | | |
| 163 |
+------------------------+--------------------------------+-----------+ |
| 164 |
| QTMATE | A bright version of the | #75a7ff | |
| 165 |
| RIAL_PRIMARYLIGHTCOLOR | primary color | | |
| 166 |
+------------------------+--------------------------------+-----------+ |
| 167 |
| QTM | Secondary color | #f5f5f5 | |
| 168 |
| ATERIAL_SECONDARYCOLOR | | | |
| 169 |
+------------------------+--------------------------------+-----------+ |
| 170 |
| QTMATERI | A bright version of the | #ffffff | |
| 171 |
| AL_SECONDARYLIGHTCOLOR | secondary color | | |
| 172 |
+------------------------+--------------------------------+-----------+ |
| 173 |
| QTMATER | A dark version of the primary | #e6e6e6 | |
| 174 |
| IAL_SECONDARYDARKCOLOR | color | | |
| 175 |
+------------------------+--------------------------------+-----------+ |
| 176 |
| QTMAT | Color for text over primary | #000000 | |
| 177 |
| ERIAL_PRIMARYTEXTCOLOR | background | | |
| 178 |
+------------------------+--------------------------------+-----------+ |
| 179 |
| QTMATER | Color for text over secondary | #000000 | |
| 180 |
| IAL_SECONDARYTEXTCOLOR | background | | |
| 181 |
+------------------------+--------------------------------+-----------+ |
| 182 |
| QTMATERIAL_THEME | Name of theme used | light | |
| 183 |
| | | _blue.xml | |
| 184 |
+------------------------+--------------------------------+-----------+ |
| 185 |
|
| 186 |
Alternative QPushButtons and custom fonts |
| 187 |
----------------------------------------- |
| 188 |
|
| 189 |
There is an ``extra`` argument for accent colors and custom fonts. |
| 190 |
|
| 191 |
.. code:: ipython3 |
| 192 |
|
| 193 |
extra = { |
| 194 |
|
| 195 |
# Button colors |
| 196 |
'danger': '#dc3545', |
| 197 |
'warning': '#ffc107', |
| 198 |
'success': '#17a2b8', |
| 199 |
|
| 200 |
# Font |
| 201 |
'font_family': 'Roboto', |
| 202 |
} |
| 203 |
|
| 204 |
apply_stylesheet(app, 'light_cyan.xml', invert_secondary=True, extra=extra) |
| 205 |
|
| 206 |
The accent colors are applied to ``QPushButton`` with the corresponding |
| 207 |
``class`` property: |
| 208 |
|
| 209 |
.. code:: ipython3 |
| 210 |
|
| 211 |
pushButton_danger.setProperty('class', 'danger') |
| 212 |
pushButton_warning.setProperty('class', 'warning') |
| 213 |
pushButton_success.setProperty('class', 'success') |
| 214 |
|
| 215 |
.. figure:: _images/extra.png |
| 216 |
:alt: extra |
| 217 |
|
| 218 |
extra |
| 219 |
|
| 220 |
Custom stylesheets |
| 221 |
------------------ |
| 222 |
|
| 223 |
Custom changes can be performed by overwriting the stylesheets, for |
| 224 |
example: |
| 225 |
|
| 226 |
.. code:: ipython3 |
| 227 |
|
| 228 |
QPushButton {{ |
| 229 |
color: {QTMATERIAL_SECONDARYCOLOR}; |
| 230 |
text-transform: none; |
| 231 |
background-color: {QTMATERIAL_PRIMARYCOLOR}; |
| 232 |
}} |
| 233 |
|
| 234 |
.big_button {{ |
| 235 |
height: 64px; |
| 236 |
}} |
| 237 |
|
| 238 |
Then, the current stylesheet can be extended just with: |
| 239 |
|
| 240 |
.. code:: ipython3 |
| 241 |
|
| 242 |
apply_stylesheet(app, theme='light_blue.xml', css_file='custom.css') |
| 243 |
|
| 244 |
The stylesheet can also be changed on runtime by: |
| 245 |
|
| 246 |
.. code:: ipython3 |
| 247 |
|
| 248 |
stylesheet = app.styleSheet() |
| 249 |
with open('custom.css') as file: |
| 250 |
app.setStyleSheet(stylesheet + file.read().format(**os.environ)) |
| 251 |
|
| 252 |
And the class style can be applied with the ``setProperty`` method: |
| 253 |
|
| 254 |
.. code:: ipython3 |
| 255 |
|
| 256 |
self.main.pushButton.setProperty('class', 'big_button') |
| 257 |
|
| 258 |
.. figure:: _images/custom.png |
| 259 |
:alt: extra |
| 260 |
|
| 261 |
extra |
| 262 |
|
| 263 |
Run examples |
| 264 |
------------ |
| 265 |
|
| 266 |
A window with almost all widgets (see the previous screenshots) are |
| 267 |
available to test all themes and **create new ones**. |
| 268 |
|
| 269 |
.. code:: ipython3 |
| 270 |
|
| 271 |
git clone https://github.com/UN-GCPDS/qt-material.git |
| 272 |
cd qt-material |
| 273 |
python setup.py install |
| 274 |
cd examples/full_features |
| 275 |
python main.py --pyside6 |
| 276 |
|
| 277 |
.. figure:: _images/theme.gif |
| 278 |
:alt: theme |
| 279 |
|
| 280 |
theme |
| 281 |
|
| 282 |
New themes |
| 283 |
---------- |
| 284 |
|
| 285 |
Do you have a custom theme? it looks good? create a `pull |
| 286 |
request <https://github.com/UN-GCPDS/qt-material/pulls>`__ in `themes |
| 287 |
folder <https://github.com/UN-GCPDS/qt-material/tree/master/qt_material/themes%3E>`__ |
| 288 |
and share it with all users. |
| 289 |
|
| 290 |
Change theme in runtime |
| 291 |
----------------------- |
| 292 |
|
| 293 |
There is a ``qt_material.QtStyleTools`` class that must be inherited |
| 294 |
along to ``QMainWindow`` for change themes in runtime using the |
| 295 |
``apply_stylesheet()`` method. |
| 296 |
|
| 297 |
.. code:: ipython3 |
| 298 |
|
| 299 |
class RuntimeStylesheets(QMainWindow, QtStyleTools): |
| 300 |
|
| 301 |
def __init__(self): |
| 302 |
super().__init__() |
| 303 |
self.main = QUiLoader().load('main_window.ui', self) |
| 304 |
|
| 305 |
self.apply_stylesheet(self.main, 'dark_teal.xml') |
| 306 |
# self.apply_stylesheet(self.main, 'light_red.xml') |
| 307 |
# self.apply_stylesheet(self.main, 'light_blue.xml') |
| 308 |
|
| 309 |
.. figure:: _images/runtime.gif |
| 310 |
:alt: run |
| 311 |
|
| 312 |
run |
| 313 |
|
| 314 |
Integrate stylesheets in a menu |
| 315 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 316 |
|
| 317 |
A custom *stylesheets menu* can be added to a project for switching |
| 318 |
across all default available themes. |
| 319 |
|
| 320 |
.. code:: ipython3 |
| 321 |
|
| 322 |
class RuntimeStylesheets(QMainWindow, QtStyleTools): |
| 323 |
|
| 324 |
def __init__(self): |
| 325 |
super().__init__() |
| 326 |
self.main = QUiLoader().load('main_window.ui', self) |
| 327 |
|
| 328 |
self.add_menu_theme(self.main, self.main.menuStyles) |
| 329 |
|
| 330 |
.. figure:: _images/runtime_menu.gif |
| 331 |
:alt: menu |
| 332 |
|
| 333 |
menu |
| 334 |
|
| 335 |
Create new themes |
| 336 |
----------------- |
| 337 |
|
| 338 |
A simple interface is available to modify a theme in runtime, this |
| 339 |
feature can be used to create a new theme, the theme file is created in |
| 340 |
the main directory as ``my_theme.xml`` |
| 341 |
|
| 342 |
.. code:: ipython3 |
| 343 |
|
| 344 |
class RuntimeStylesheets(QMainWindow, QtStyleTools): |
| 345 |
|
| 346 |
def __init__(self): |
| 347 |
super().__init__() |
| 348 |
self.main = QUiLoader().load('main_window.ui', self) |
| 349 |
|
| 350 |
self.show_dock_theme(self.main) |
| 351 |
|
| 352 |
.. figure:: _images/runtime_dock.gif |
| 353 |
:alt: dock |
| 354 |
|
| 355 |
dock |
| 356 |
|
| 357 |
A full set of examples are available in the `exmaples |
| 358 |
directory <https://github.com/UN-GCPDS/qt-material/blob/master/examples/runtime/>`__ |
| 359 |
|
| 360 |
Export theme |
| 361 |
------------ |
| 362 |
|
| 363 |
This feature able to use ``qt-material`` themes into ``Qt`` |
| 364 |
implementations using only local files. |
| 365 |
|
| 366 |
.. code:: ipython3 |
| 367 |
|
| 368 |
from qt_material import export_theme |
| 369 |
|
| 370 |
extra = { |
| 371 |
|
| 372 |
# Button colors |
| 373 |
'danger': '#dc3545', |
| 374 |
'warning': '#ffc107', |
| 375 |
'success': '#17a2b8', |
| 376 |
|
| 377 |
# Font |
| 378 |
'font_family': 'monoespace', |
| 379 |
'font_size': '13px', |
| 380 |
'line_height': '13px', |
| 381 |
|
| 382 |
# Density Scale |
| 383 |
'density_scale': '0', |
| 384 |
|
| 385 |
# environ |
| 386 |
'pyside6': True, |
| 387 |
'linux': True, |
| 388 |
|
| 389 |
} |
| 390 |
|
| 391 |
export_theme(theme='dark_teal.xml', |
| 392 |
qss='dark_teal.qss', |
| 393 |
rcc='resources.rcc', |
| 394 |
output='theme', |
| 395 |
prefix='icon:/', |
| 396 |
invert_secondary=False, |
| 397 |
extra=extra, |
| 398 |
) |
| 399 |
|
| 400 |
This script will generate both ``dark_teal.qss`` and ``resources.rcc`` |
| 401 |
and a folder with all theme icons called ``theme``. |
| 402 |
|
| 403 |
The files generated can be integrated into a ``PySide6`` application |
| 404 |
just with: |
| 405 |
|
| 406 |
.. code:: ipython3 |
| 407 |
|
| 408 |
import sys |
| 409 |
|
| 410 |
from PySide6 import QtWidgets |
| 411 |
from PySide6.QtCore import QDir |
| 412 |
from __feature__ import snake_case, true_property |
| 413 |
|
| 414 |
# Create application |
| 415 |
app = QtWidgets.QApplication(sys.argv) |
| 416 |
|
| 417 |
# Load styles |
| 418 |
with open('dark_teal.qss', 'r') as file: |
| 419 |
app.style_sheet = file.read() |
| 420 |
|
| 421 |
# Load icons |
| 422 |
QDir.add_search_path('icon', 'theme') |
| 423 |
|
| 424 |
# App |
| 425 |
window = QtWidgets.QMainWindow() |
| 426 |
checkbox = QtWidgets.QCheckBox(window) |
| 427 |
checkbox.text = 'CheckBox' |
| 428 |
window.show() |
| 429 |
app.exec() |
| 430 |
|
| 431 |
This files can also be used into non ``Python`` environs like ``C++``. |
| 432 |
|
| 433 |
Density scale |
| 434 |
------------- |
| 435 |
|
| 436 |
The ``extra`` arguments also include an option to set the **density |
| 437 |
scale**, by default is ``0``. |
| 438 |
|
| 439 |
.. code:: ipython3 |
| 440 |
|
| 441 |
extra = { |
| 442 |
|
| 443 |
# Density Scale |
| 444 |
'density_scale': '-2', |
| 445 |
} |
| 446 |
|
| 447 |
apply_stylesheet(app, 'default', invert_secondary=False, extra=extra) |
| 448 |
|
| 449 |
.. figure:: _images/density/density.gif |
| 450 |
:alt: dock |
| 451 |
|
| 452 |
dock |
| 453 |
|
| 454 |
Troubleshoots |
| 455 |
------------- |
| 456 |
|
| 457 |
QMenu |
| 458 |
~~~~~ |
| 459 |
|
| 460 |
``QMenu`` has multiple rendering for each Qt backend, and for each |
| 461 |
operating system. Even can be related with the style, like |
| 462 |
`fusion <https://doc.qt.io/qt-5/qtquickcontrols2-fusion.html>`__. Then, |
| 463 |
the ``extra`` argument also supports\ ``QMenu`` parameters to configure |
| 464 |
this widgest for specific combinations. This options are not affected by |
| 465 |
**density scale**. |
| 466 |
|
| 467 |
.. code:: ipython3 |
| 468 |
|
| 469 |
extra['QMenu'] = { |
| 470 |
'height': 50, |
| 471 |
'padding': '50px 50px 50px 50px', # top, right, bottom, left |
| 472 |
} |