PyQt5 ComboBox - Different border color when mouse hover

PyQt5 ComboBox - Different border color when mouse hover

To change the border color of a QComboBox when the mouse hovers over it in PyQt5, you can use stylesheets. The QComboBox::hover pseudo-state selector allows you to style the widget when it's being hovered over.

Here's an example of how to achieve this:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox class MyApp(QWidget): def __init__(self): super(MyApp, self).__init__() layout = QVBoxLayout(self) combobox = QComboBox(self) combobox.addItems(['Option 1', 'Option 2', 'Option 3']) # Set the stylesheet combobox.setStyleSheet(""" QComboBox { border: 2px solid gray; padding: 1px 18px 1px 3px; min-width: 6em; } QComboBox:hover { border-color: red; } QComboBox QAbstractItemView { border: 2px solid red; } """) layout.addWidget(combobox) self.setLayout(layout) self.setWindowTitle('PyQt5 ComboBox Hover Border') self.show() app = QApplication(sys.argv) window = MyApp() sys.exit(app.exec_()) 

In the above code:

  • The standard QComboBox has a gray border.
  • When you hover over the QComboBox, the border color changes to red.
  • The dropdown view (QAbstractItemView) of the QComboBox also has a red border when opened.

You can adjust the stylesheet as per your design requirements.


More Tags

shortcut xunit powerbi httppostedfilebase ssid postgresql-9.1 pocketpc enums cordova serverxmlhttp

More Programming Guides

Other Guides

More Programming Examples