Qt theme in pure qss
-
Qt Theme is a pure qss project that can easily improve the style of existing projects.
Supports C++, PyQt5, PyQt6, PySide2, PySide6, and is published on GitHub Pages as WebAssembly.
Here is a demo of its use on Python, first install it:
pip install QtTheme
Alternatively, without installing QtTheme, you can export a single-style qrc resource package through the online website. Use RCC to add it to your project。
Let AI write an UI for me as an style example:
# Generated by ChatGPT import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QLineEdit, QComboBox, QCheckBox, QRadioButton class MyWindow(QDialog): def __init__(self): super().__init__() self.setWindowTitle("PyQt5 Widgets Example") self.setGeometry(100, 100, 400, 300) # Create layouts main_layout = QVBoxLayout() # Vertical layout for the whole window form_layout = QVBoxLayout() # Layout for the form # Add a QLabel and QLineEdit (text input) self.label = QLabel("Enter your name:", self) self.text_input = QLineEdit(self) form_layout.addWidget(self.label) form_layout.addWidget(self.text_input) # Add a QComboBox (dropdown) self.combo_label = QLabel("Select your favorite color:", self) self.combo_box = QComboBox(self) self.combo_box.addItems(["Red", "Green", "Blue", "Yellow"]) form_layout.addWidget(self.combo_label) form_layout.addWidget(self.combo_box) # Add a QCheckBox self.check_box = QCheckBox("I agree to the terms and conditions", self) form_layout.addWidget(self.check_box) # Add a QRadioButton (grouped) self.radio_label = QLabel("Choose your preferred language:", self) self.radio_button_english = QRadioButton("English", self) self.radio_button_french = QRadioButton("French", self) self.radio_button_spanish = QRadioButton("Spanish", self) # Add the radio buttons to a horizontal layout radio_layout = QHBoxLayout() radio_layout.addWidget(self.radio_button_english) radio_layout.addWidget(self.radio_button_french) radio_layout.addWidget(self.radio_button_spanish) form_layout.addWidget(self.radio_label) form_layout.addLayout(radio_layout) # Add a QPushButton self.submit_button = QPushButton("Submit", self) self.submit_button.clicked.connect(self.on_button_click) # Add an exit button self.exit_button = QPushButton("Exit", self) button_layout = QHBoxLayout() button_layout.addWidget(self.submit_button) button_layout.addWidget(self.exit_button) # Add the form layout to the main layout main_layout.addLayout(form_layout) main_layout.addLayout(button_layout) # Set the window layout self.setLayout(main_layout) def on_button_click(self): name = self.text_input.text() favorite_color = self.combo_box.currentText() is_agree = self.check_box.isChecked() preferred_language = "None" if self.radio_button_english.isChecked(): preferred_language = "English" elif self.radio_button_french.isChecked(): preferred_language = "French" elif self.radio_button_spanish.isChecked(): preferred_language = "Spanish" print(f"Name: {name}") print(f"Favorite Color: {favorite_color}") print(f"Agreed to Terms: {is_agree}") print(f"Preferred Language: {preferred_language}") if __name__ == "__main__": app = QApplication(sys.argv) window = MyWindow() window.show() sys.exit(app.exec_())
try to run it:
Import QtTheme, then read and set the style for the root widget through the Qt resource system:
import QtTheme.PyQt5 from PyQt5.QtCore import QFile class MyWindow(QDialog): def __init__(self): # ... qss = QFile(":/QtTheme/theme/Flat/Dark/Blue/Pink.qss") qss.open(QFile.OpenModeFlag.ReadOnly) self.setStyleSheet(qss.readAll().data().decode())
Finally, set the color for the widgets through
QWidget.setProperty
as needed:class MyWindow(QDialog): def __init__(self): # ... qss = QFile(":/QtTheme/theme/Flat/Dark/Blue/Pink.qss") qss.open(QFile.OpenModeFlag.ReadOnly) self.setStyleSheet(qss.readAll().data().decode()) self.submit_button.setProperty("Color", "Primary") self.exit_button.setProperty("Color", "Danger")
-