PyQt5 - Setting background color to non editable OFF state ComboBox when pressed

PyQt5 - Setting background color to non editable OFF state ComboBox when pressed

To set the background color for a QComboBox in its non-editable (read-only) state when it's off (not dropped down) and pressed, you can use the PyQt5's stylesheet (QSS) method.

Here's a step-by-step guide:

  1. Create a QComboBox.
  2. Make sure it's in non-editable mode.
  3. Set the desired background color using QSS for the :pressed state when the combo box is not open (!open) and not editable (!editable).

Here's a sample code to illustrate this:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QComboBox, QVBoxLayout class App(QWidget): def __init__(self): super().__init__() # Create a ComboBox self.combo = QComboBox(self) self.combo.setEditable(False) # Ensure the ComboBox is not editable self.combo.addItems(["Option 1", "Option 2", "Option 3"]) # Layout layout = QVBoxLayout(self) layout.addWidget(self.combo) # Styling self.setStyleSheet(""" /* This will style the non-editable QComboBox in its off state when pressed. The "!editable" selector targets the ComboBox in its non-editable state. The "!open" selector ensures it's in the off state (not dropped down). The ":pressed" selector targets the pressed state. */ QComboBox:!editable:!open:pressed { background-color: #FFD700; # Example color: gold } /* This is optional: Style for the non-editable ComboBox */ QComboBox:!editable { padding: 5px; } """) if __name__ == '__main__': app = QApplication(sys.argv) ex = App() ex.show() sys.exit(app.exec_()) 

Replace #FFD700 with your desired color in hexadecimal format. The above color is just an example (gold). Adjust as per your needs.


More Tags

blank-line pinchzoom duration actionmode oledbdataadapter regexp-substr deploying airflow-scheduler output eclipse-plugin

More Programming Guides

Other Guides

More Programming Examples