PyQt5 - Setting background color to editable ComboBox when pressed

PyQt5 - Setting background color to editable ComboBox when pressed

In PyQt5, you can use Qt Style Sheets to customize the appearance of widgets, similar to how CSS is used for styling HTML elements. For setting the background color of an editable QComboBox when it is pressed, you can use the :pressed pseudo-state selector in combination with the QComboBox:editable selector.

Here is an example:

import sys
from PyQt5.QtWidgets import QApplication, QComboBox, QVBoxLayout, QWidget

app = QApplication(sys.argv)

window = QWidget()
layout = QVBoxLayout()

combo_box = QComboBox()
combo_box.setEditable(True)
combo_box.addItem("Option 1")
combo_box.addItem("Option 2")
combo_box.addItem("Option 3")

layout.addWidget(combo_box)

window.setLayout(layout)

# Set stylesheet
window.setStyleSheet("""
    QComboBox:editable:pressed {
        background-color: yellow;
    }
""")

window.show()
sys.exit(app.exec_())

In this example, the background color of the editable QComboBox will turn yellow when the widget is pressed. The Qt Style Sheet string QComboBox:editable:pressed specifies that this styling applies to QComboBox widgets that are editable and currently in a pressed state.

You can modify the Qt Style Sheet string to suit your particular needs, such as using different colors or additional styling attributes.


More Tags

composer-php jupyter-notebook swift3 x11 mobile-application coalesce tsx android-dialogfragment shebang transform

More Programming Guides

Other Guides

More Programming Examples