PyQt5 - Background color to non editable ComboBox when mouse hover

PyQt5 - Background color to non editable ComboBox when mouse hover

If you want to set the background color of a non-editable QComboBox when the mouse hovers over it, you can use the QComboBox's stylesheet in conjunction with the :hover pseudo-state. This way, you can define specific styles to be applied when the widget is being hovered by the mouse.

Here's how you can set a background color to a non-editable QComboBox when the mouse hovers over it using PyQt5:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox class AppDemo(QWidget): def __init__(self): super().__init__() self.setWindowTitle('QComboBox Hover Example') layout = QVBoxLayout(self) combo = QComboBox(self) combo.addItems(['Option 1', 'Option 2', 'Option 3']) combo.setEditable(False) # Setting the hover stylesheet for the non-editable QComboBox combo.setStyleSheet(""" QComboBox:!editable:hover { background-color: lightgreen; } """) layout.addWidget(combo) app = QApplication(sys.argv) demo = AppDemo() demo.show() sys.exit(app.exec_()) 

In the example above, the QComboBox background color changes to lightgreen when you hover over it with the mouse. The :hover pseudo-state is used in combination with the !editable selector to apply the style only to non-editable combo boxes.


More Tags

coupon prometheus-alertmanager normal-distribution algorithms background-image toastr postgis oauth-2.0 monodevelop werkzeug

More Programming Guides

Other Guides

More Programming Examples