PyQt5 - Set skin to combobox when in OFF state and pressed

PyQt5 - Set skin to combobox when in OFF state and pressed

To apply a specific style or "skin" to a QComboBox when it's in the OFF state and when it's pressed, you'll need to utilize the Qt Style Sheets (QSS) with the appropriate pseudo-states.

Specifically, for a non-editable QComboBox:

  1. !editable pseudo-state corresponds to the OFF state.
  2. ::drop-down:pressed sub-control and pseudo-state correspond to when the drop-down arrow is pressed.

Here's an example demonstrating how to set a specific style for a QComboBox when it's in the OFF state and when the drop-down arrow is pressed:

import sys from PyQt5.QtWidgets import QApplication, QComboBox, QVBoxLayout, QWidget class App(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() combo = QComboBox(self) combo.addItems(['Option 1', 'Option 2', 'Option 3']) combo.setEditable(False) layout.addWidget(combo) # Set the style for the QComboBox combo.setStyleSheet(""" /* When ComboBox is in OFF state */ QComboBox:!editable { background-color: lightblue; } /* When drop-down arrow is pressed */ QComboBox:!editable::drop-down:pressed { background-color: lightgreen; } """) self.setLayout(layout) self.setWindowTitle('QComboBox Styling') self.setGeometry(100, 100, 300, 200) self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_()) 

In this example:

  • The QComboBox:!editable selector ensures styling is applied only to non-editable combo boxes. We set a light blue background color for the OFF state.
  • The QComboBox:!editable::drop-down:pressed selector is used to apply a style when the drop-down arrow of the combo box is pressed. We set a light green background color when pressed.

Adjust the properties and values in the style sheet to suit your desired appearance.


More Tags

browser aforge elastic-stack numerical react-native-navigation one-to-many xlwt progress remote-connection virtualbox

More Programming Guides

Other Guides

More Programming Examples