QDarkStyleSheet
QDarkStyleSheet (qdarkstyle) provides a comprehensive dark and light stylesheet for C++/Python Qt applications. It offers a consistent, modern theme for your GUI, compatible with both PyQt5 and PySide2. The current version is 3.2.3, and it receives regular minor updates with occasional major releases for significant structural changes and new features.
Common errors
-
ModuleNotFoundError: No module named 'qdarkstyle'
cause The qdarkstyle library is not installed in your current Python environment.fixRun: `pip install qdarkstyle` -
ModuleNotFoundError: No module named 'PyQt5'
cause You are trying to use PyQt5 but it is not installed. This is a prerequisite for qdarkstyle.fixRun: `pip install PyQt5` (or `pip install PySide2` if you prefer PySide2). -
AttributeError: module 'qdarkstyle' has no attribute 'load_stylesheet_pyqt5'
cause This usually means you're trying to use a binding-specific function (e.g., for PyQt5) but either an older version of qdarkstyle is installed, or you're using a generic loader that doesn't support the specific function, or the wrong binding's function name is used.fixEnsure `qdarkstyle` is updated (`pip install --upgrade qdarkstyle`) and verify you are calling the correct function for your binding (e.g., `load_stylesheet_pyqt5()` for PyQt5 or `load_stylesheet_pyside2()` for PySide2).
Warnings
- breaking QDarkStyleSheet dropped support for Python 2 and Qt4. Attempting to use it with these older versions will result in errors or unexpected behavior.
- breaking Version 3.0.0 introduced a new internal structure, improved dark and new light palettes. If you were relying on deeply integrated custom styling or internal API calls prior to 3.0.0, you might need to adapt your code.
- gotcha QDarkStyleSheet provides stylesheets but does not install Qt bindings (PyQt5 or PySide2) itself. You must explicitly install one of these bindings for `qdarkstyle` to function.
- gotcha Ensure you are using the correct `load_stylesheet_pyqt5()` or `load_stylesheet_pyside2()` function that matches your installed Qt binding.
Install
-
pip install qdarkstyle
Imports
- load_stylesheet_pyqt5
app.setStyleSheet(qdarkstyle.load_stylesheet())
import qdarkstyle app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
- load_stylesheet_pyside2
app.setStyleSheet(qdarkstyle.load_stylesheet())
import qdarkstyle app.setStyleSheet(qdarkstyle.load_stylesheet_pyside2())
Quickstart
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt
import qdarkstyle
def main():
app = QApplication(sys.argv)
# Apply the dark stylesheet for PyQt5
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
# For PySide2, use:
# app.setStyleSheet(qdarkstyle.load_stylesheet_pyside2())
# Create a simple main window
main_window = QMainWindow()
main_window.setWindowTitle("QDarkStyle Demo")
main_window.setGeometry(100, 100, 400, 200)
central_widget = QWidget()
main_window.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
button = QPushButton("Styled Button", main_window)
layout.addWidget(button, alignment=Qt.AlignCenter)
main_window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()