PyQt5 - Adding border to OFF state ComboBox when mouse hover

PyQt5 - Adding border to OFF state ComboBox when mouse hover

To add a border to a QComboBox in its OFF state (i.e., when it's not expanded) when the mouse hovers over it in PyQt5, you can use the QComboBox's style sheet with the appropriate pseudo-states.

The relevant pseudo-states for this task are:

  • :hover �C when the mouse hovers over the widget.
  • :!editable �C when the QComboBox is in the OFF or non-editable state.

Here's an example to demonstrate this:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox app = QApplication(sys.argv) window = QWidget() layout = QVBoxLayout() combo_box = QComboBox() combo_box.addItems(['Option 1', 'Option 2', 'Option 3']) # Set style sheet for QComboBox combo_box.setStyleSheet(""" QComboBox:!editable:hover { border: 2px solid red; } """) layout.addWidget(combo_box) window.setLayout(layout) window.show() sys.exit(app.exec_()) 

In this code, the style sheet is set such that when the QComboBox is hovered over by the mouse and it's in the OFF state (:!editable), it will show a red border.


More Tags

ghostscript machine-code color-depth icalendar decode android-styles impala pygtk http-proxy tld

More Programming Guides

Other Guides

More Programming Examples