PyQt5 ComboBox - Different border size when non-editable and mouse hover

PyQt5 ComboBox - Different border size when non-editable and mouse hover

If you want to set a different border size for a QComboBox when it is non-editable and when the mouse hovers over it in PyQt5, you can achieve this using QSS (Qt Style Sheets).

The following example demonstrates how you can set a border size of 3 pixels for a non-editable QComboBox when the mouse hovers over it:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox class AppDemo(QWidget): def __init__(self): super().__init__() self.setWindowTitle('ComboBox Border Demo') self.setGeometry(100, 100, 300, 150) layout = QVBoxLayout() combo = QComboBox(self) combo.setEditable(False) # make ComboBox non-editable combo.addItems(['Option 1', 'Option 2', 'Option 3']) layout.addWidget(combo) # Set the QSS style combo.setStyleSheet(""" QComboBox:!editable:hover { border: 3px solid red; } QComboBox::drop-down:hover { border: 0px; } """) self.setLayout(layout) app = QApplication(sys.argv) demo = AppDemo() demo.show() sys.exit(app.exec_()) 

Here's a breakdown of the QSS:

  • QComboBox:!editable:hover: This targets QComboBox widgets that are not editable (!editable) and are being hovered over (hover). We set the border size to 3 pixels and color it red.

  • QComboBox::drop-down:hover: This targets the drop-down button of the QComboBox when it's hovered over. We reset the border to 0 pixels to make sure the drop-down button doesn't have an exaggerated border.

When you run this code, hovering over the non-editable QComboBox will show a red border of 3 pixels. Adjust the styles as per your needs.


More Tags

user-controls facet-grid intersection-observer jenkins-blueocean propertygrid kingfisher double-submit-problem simplebar message

More Programming Guides

Other Guides

More Programming Examples