PyQt5 - Set skin to editable off state combobox when pressed

PyQt5 - Set skin to editable off state combobox when pressed

To change the skin or appearance of a QComboBox when it's in an "editable off" state and pressed, you can use Qt's Stylesheet mechanism. Specifically, you can target the QComboBox when it's not editable using the QComboBox:!editable selector and combine it with the :pressed pseudo-state.

Here's an example that demonstrates how to change the background color of a QComboBox to light blue when it's in a non-editable state and pressed:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox 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']) # The combo box is non-editable by default, but you can make it explicit with the following: combo.setEditable(False) # Set the styles for the combobox combo.setStyleSheet(""" /* Style when combobox is non-editable and pressed */ QComboBox:!editable:pressed { background-color: lightblue; } """) layout.addWidget(combo) self.setLayout(layout) self.setGeometry(100, 100, 300, 150) app = QApplication(sys.argv) ex = App() ex.show() sys.exit(app.exec_()) 

In this code:

  • The QComboBox style is changed when it's not editable and pressed using the QComboBox:!editable:pressed selector. The background color is set to lightblue.

You can customize this style as per your requirements by adjusting the properties in the combo.setStyleSheet() method.


More Tags

firefox-addon aws-powershell mp3 intervals manifest socketserver iso-8859-1 xlsx submit facebook

More Programming Guides

Other Guides

More Programming Examples