PyQt5 - Add border to lineedit part of non editable ComboBox

PyQt5 - Add border to lineedit part of non editable ComboBox

If you want to add a border specifically to the QLineEdit part of a non-editable QComboBox, you can use the QSS (Qt Style Sheets) to achieve that. The QComboBox when non-editable will render the current item using a QComboBox::item sub-control.

To add a border to the QLineEdit part, you can apply styles directly to QComboBox but avoid affecting the drop-down list.

Here's an example to illustrate this:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox class AppDemo(QWidget): def __init__(self): super().__init__() layout = QVBoxLayout() comboBox = QComboBox(self) comboBox.addItems(['Option 1', 'Option 2', 'Option 3']) # The style is applied to the QComboBox directly, which primarily styles the QLineEdit part. comboBox.setStyleSheet(''' QComboBox { border: 2px solid red; } QComboBox QAbstractItemView { border: 0px; } ''') layout.addWidget(comboBox) self.setLayout(layout) app = QApplication(sys.argv) demo = AppDemo() demo.show() sys.exit(app.exec_()) 

In the above code:

  1. We create a QComboBox and add some items to it.
  2. We use QSS to style the QComboBox. We've set a red border using border: 2px solid red; for the QLineEdit part.
  3. We then set border: 0px; for QComboBox QAbstractItemView to ensure that the drop-down list doesn't inherit the border from the main combo box.

Note: Adjustments might be required depending on the specific styling and visuals you have in mind. The provided example gives a basic idea of how to apply borders to the QLineEdit part of the QComboBox.


More Tags

filenotfoundexception linker jakarta-ee implements transpose python-module plsqldeveloper exoplayer pipenv datetime-parsing

More Programming Guides

Other Guides

More Programming Examples