Results 1 to 3 of 3

Thread: Qt theme in pure qss

  1. #1
    Join Date
    Feb 2025
    Posts
    2
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Qt theme in pure qss

    https://github.com/hubenchang0515/QtTheme

    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:

    Qt Code:
    1. # Generated by ChatGPT
    2. import sys
    3. from PyQt5.QtCore import Qt
    4.  
    5. class MyWindow(QDialog):
    6. def __init__(self):
    7. super().__init__()
    8. self.setWindowTitle("PyQt5 Widgets Example")
    9. self.setGeometry(100, 100, 400, 300)
    10.  
    11. # Create layouts
    12. main_layout = QVBoxLayout() # Vertical layout for the whole window
    13. form_layout = QVBoxLayout() # Layout for the form
    14.  
    15. # Add a QLabel and QLineEdit (text input)
    16. self.label = QLabel("Enter your name:", self)
    17. self.text_input = QLineEdit(self)
    18.  
    19. form_layout.addWidget(self.label)
    20. form_layout.addWidget(self.text_input)
    21.  
    22. # Add a QComboBox (dropdown)
    23. self.combo_label = QLabel("Select your favorite color:", self)
    24. self.combo_box = QComboBox(self)
    25. self.combo_box.addItems(["Red", "Green", "Blue", "Yellow"])
    26.  
    27. form_layout.addWidget(self.combo_label)
    28. form_layout.addWidget(self.combo_box)
    29.  
    30. # Add a QCheckBox
    31. self.check_box = QCheckBox("I agree to the terms and conditions", self)
    32.  
    33. form_layout.addWidget(self.check_box)
    34.  
    35. # Add a QRadioButton (grouped)
    36. self.radio_label = QLabel("Choose your preferred language:", self)
    37. self.radio_button_english = QRadioButton("English", self)
    38. self.radio_button_french = QRadioButton("French", self)
    39. self.radio_button_spanish = QRadioButton("Spanish", self)
    40.  
    41. # Add the radio buttons to a horizontal layout
    42. radio_layout = QHBoxLayout()
    43. radio_layout.addWidget(self.radio_button_english)
    44. radio_layout.addWidget(self.radio_button_french)
    45. radio_layout.addWidget(self.radio_button_spanish)
    46.  
    47. form_layout.addWidget(self.radio_label)
    48. form_layout.addLayout(radio_layout)
    49.  
    50. # Add a QPushButton
    51. self.submit_button = QPushButton("Submit", self)
    52. self.submit_button.clicked.connect(self.on_button_click)
    53.  
    54. # Add an exit button
    55. self.exit_button = QPushButton("Exit", self)
    56.  
    57. button_layout = QHBoxLayout()
    58. button_layout.addWidget(self.submit_button)
    59. button_layout.addWidget(self.exit_button)
    60.  
    61. # Add the form layout to the main layout
    62. main_layout.addLayout(form_layout)
    63. main_layout.addLayout(button_layout)
    64.  
    65. # Set the window layout
    66. self.setLayout(main_layout)
    67.  
    68. def on_button_click(self):
    69. name = self.text_input.text()
    70. favorite_color = self.combo_box.currentText()
    71. is_agree = self.check_box.isChecked()
    72. preferred_language = "None"
    73.  
    74. if self.radio_button_english.isChecked():
    75. preferred_language = "English"
    76. elif self.radio_button_french.isChecked():
    77. preferred_language = "French"
    78. elif self.radio_button_spanish.isChecked():
    79. preferred_language = "Spanish"
    80.  
    81. print(f"Name: {name}")
    82. print(f"Favorite Color: {favorite_color}")
    83. print(f"Agreed to Terms: {is_agree}")
    84. print(f"Preferred Language: {preferred_language}")
    85.  
    86. if __name__ == "__main__":
    87. app = QApplication(sys.argv)
    88. window = MyWindow()
    89. window.show()
    90. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    try to run it:



    Import QtTheme, then read and set the style for the root widget through the Qt resource system:

    Qt Code:
    1. import QtTheme.PyQt5
    2. from PyQt5.QtCore import QFile
    3.  
    4. class MyWindow(QDialog):
    5. def __init__(self):
    6. # ...
    7. qss = QFile(":/QtTheme/theme/Flat/Dark/Blue/Pink.qss")
    8. qss.open(QFile.OpenModeFlag.ReadOnly)
    9. self.setStyleSheet(qss.readAll().data().decode())
    To copy to clipboard, switch view to plain text mode 



    Finally, set the color for the widgets through QWidget.setProperty as needed:

    Qt Code:
    1. class MyWindow(QDialog):
    2. def __init__(self):
    3. # ...
    4. qss = QFile(":/QtTheme/theme/Flat/Dark/Blue/Pink.qss")
    5. qss.open(QFile.OpenModeFlag.ReadOnly)
    6. self.setStyleSheet(qss.readAll().data().decode())
    7.  
    8. self.submit_button.setProperty("Color", "Primary")
    9. self.exit_button.setProperty("Color", "Danger")
    To copy to clipboard, switch view to plain text mode 


  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,298
    Thanks
    312
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt theme in pure qss

    You would get more widespread use of your project if you licensed it as LGPL instead of GPL.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    hubenchang (16th February 2025)

  4. #3
    Join Date
    Feb 2025
    Posts
    2
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Qt theme in pure qss

    The generated qss files are licensed as MIT

  5. The following user says thank you to hubenchang for this useful post:

    d_stranz (17th February 2025)

Similar Threads

  1. Help out on Qt BG color in GTK (theme)
    By budi in forum Qt-based Software
    Replies: 0
    Last Post: 17th July 2021, 12:35
  2. Icon from theme
    By wirasto in forum Qt Programming
    Replies: 1
    Last Post: 22nd December 2009, 13:42
  3. How to change theme of a window
    By sudheer168 in forum Qt Programming
    Replies: 1
    Last Post: 20th November 2009, 14:46
  4. Change Qt theme while running
    By Macok in forum Qt Programming
    Replies: 7
    Last Post: 12th February 2009, 20:57
  5. question about plastique theme
    By coder1985 in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 21:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.