PyQt5 - Different border width to lineedit part on mouse hover (for non editable Combo box)

PyQt5 - Different border width to lineedit part on mouse hover (for non editable Combo box)

To give a different border width to the QLineEdit part of a non-editable QComboBox when the mouse hovers over it, you can use Qt Style Sheets.

Here's a simple example to demonstrate how you can achieve this effect:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox class App(QWidget): def __init__(self): super().__init__() self.title = 'PyQt5 �C ComboBox Styling' self.left = 100 self.top = 100 self.width = 640 self.height = 480 self.initUI() def initUI(self): self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) layout = QVBoxLayout() combo = QComboBox(self) combo.addItems(["Option 1", "Option 2", "Option 3"]) # Set the ComboBox as non-editable combo.setEditable(False) # Apply a stylesheet to the ComboBox combo.setStyleSheet(""" QComboBox { border: 2px solid gray; padding: 2px; } QComboBox:hover { border-width: 4px 4px 4px 0px; } QComboBox::drop-down { border-width: 0px; } """) layout.addWidget(combo) self.setLayout(layout) self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_()) 

In the provided stylesheet:

  • The default QComboBox has a border of 2 pixels width all around.
  • When you hover over the QComboBox, only the QLineEdit part (left three sides) gets a border width of 4 pixels. The right side border (drop-down arrow side) remains without a border due to the border-width: 4px 4px 4px 0px; line.
  • QComboBox::drop-down ensures that the drop-down arrow does not get a border.

This will give the effect of a different border width for the QLineEdit part of the QComboBox when the mouse hovers over it. Adjust the styles as necessary to fit your specific design requirements.


More Tags

msysgit protocol-handler sql-null react-navigation cpu-registers phasset api unsafe carousel react-leaflet

More Programming Guides

Other Guides

More Programming Examples