Qstylizer
raw JSON → 0.2.4 verified Fri May 01 auth: no python maintenance
Qstylizer is a Python library that generates Qt stylesheets (QSS) programmatically. It provides a clean, Pythonic API for creating and composing styles for PyQt4/5 and PySide1/2 applications. Current version 0.2.4, with irregular releases; last release in 2018.
pip install qstylizer Common errors
error ImportError: cannot import name 'StyleSheet' from 'qstylizer' ↓
cause StyleSheet is not exposed at the top-level; it lives in qstylizer.style.
fix
Use: from qstylizer.style import StyleSheet
error AttributeError: 'StyleSheet' object has no attribute 'QPushButton' ↓
cause The StyleSheet object dynamically creates attributes based on widget names; if the attribute doesn't exist, it means the widget name is misspelled or not yet accessed.
fix
Ensure you use the exact class name (e.g., QPushButton) and call the attribute correctly: ss.QPushButton.setBackgroundColor('red')
Warnings
gotcha StyleSheet objects are mutable and reuse the same internal dictionary. Modifying a returned StyleSheet (e.g., from a property) may affect the original. Use .copy() if you need an independent instance. ↓
fix Call .copy() on the StyleSheet instance before mutating if you need isolation.
gotcha Property names use camelCase style (e.g., setBackgroundColor, setBorderRadius) but output CSS properties follow Qt conventional kebab-case after compilation. There is no built-in validation; misspellings will pass silently and produce no effect. ↓
fix Double-check property names in the generated QSS; use Qt documentation for correct CSS property names.
gotcha The library does not support pseudo-states (e.g., :hover) or sub-controls (e.g., ::drop-down) directly. Complex selectors must be built manually, which often leads to confusion. ↓
fix Use __setitem__ with raw selectors: ss['QPushButton:hover']['background-color'] = 'blue'
Imports
- StyleSheet wrong
from qstylizer import StyleSheetcorrectfrom qstylizer.style import StyleSheet
Quickstart
from qstylizer.style import StyleSheet
# Create a stylesheet for a QPushButton
ss = StyleSheet()
ss.QPushButton.setBackgroundColor('red')
ss.QPushButton.setColor('white')
ss.QPushButton.setBorderRadius('5px')
# Compile to QSS string
qss = ss.toString()
print(qss)
# Output: QPushButton { background-color: red; color: white; border-radius: 5px; }