PyQt5 - Adding border to lineedit part of non-editable combobox when mouse hover

PyQt5 - Adding border to lineedit part of non-editable combobox when mouse hover

To add a border to the QLineEdit part of a non-editable QComboBox when the mouse hovers over it in PyQt5, you can use the QComboBox::hover pseudo-state in combination with stylesheets.

Here's a step-by-step guide on how to do this:

  1. Import the necessary modules.
  2. Set up the application and the main window.
  3. Create a QComboBox, populate it with items, and make it non-editable.
  4. Apply a stylesheet to the QComboBox to change the border of the QLineEdit part when hovered.
  5. Show the main window and run the application.

Here's a code example:

import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QComboBox class App(QMainWindow): def __init__(self): super().__init__() # Initialize the QComboBox combo_box = QComboBox(self) combo_box.addItems(["Option 1", "Option 2", "Option 3"]) # Make the QComboBox non-editable combo_box.setEditable(False) # Set up the stylesheet for the QComboBox combo_box.setStyleSheet(""" QComboBox:hover { border: 2px solid red; } """) # Set the position and show the main window self.setGeometry(100, 100, 300, 200) combo_box.move(50, 50) self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_()) 

In this example, when you hover the mouse over the QComboBox, its border will change to a 2-pixel wide red border. Adjust the stylesheet as needed to achieve the desired appearance.


More Tags

azure-servicebus-topics kendo-chart strikethrough conditional-statements bufferedreader content-security-policy gmail-api tinymce predict appdelegate

More Programming Guides

Other Guides

More Programming Examples