qt-material

repository·master·Indexed 25 days ago

https://github.com/un-gcpds/qt-material

A 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.

Tokens
1.9K
Snippets
9
Records
11
Agent score
34%

What's inside qt-material

  1. Extend stylesheets with custom CSS

    master
    You can extend the applied theme by providing a path to a custom CSS file using the css_file argument in apply_stylesheet. You can also use placeholders like {QTMATERIAL_PRIMARYCOLOR} in your CSS to reference the current theme's colors.
  2. Apply a stylesheet to a Qt application

    master

    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_()
  3. Create a custom theme using XML

    master

    You 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>
  4. Export theme for use in C++ or other environments

    master

    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,
    )
  5. Set density scale

    master

    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)
  6. Change themes at runtime

    master
    To enable dynamic theme switching, your main window class must inherit from both 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).
  7. Configure accent colors and fonts via the extra argument

    master

    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')
  8. Reference: Environment variables for theme colors

    master

    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                       |
  9. Reference: Extra configuration for QMenu

    master

    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
    }