PyQt5 ComboBox - Different border width when non-editable

PyQt5 ComboBox - Different border width when non-editable

To set a different border width for a non-editable QComboBox in PyQt5, you can use stylesheets. With stylesheets, you can apply a specific style based on the properties of a widget. In this case, you can use the editable property to apply a different border width based on whether the QComboBox is editable or not.

Here's a simple example:

import sys from PyQt5.QtWidgets import QApplication, QComboBox, QVBoxLayout, QWidget app = QApplication(sys.argv) window = QWidget() layout = QVBoxLayout() combo = QComboBox() combo.addItems(['Option 1', 'Option 2', 'Option 3']) layout.addWidget(combo) combo2 = QComboBox() combo2.addItems(['Option A', 'Option B', 'Option C']) combo2.setEditable(True) layout.addWidget(combo2) # Set stylesheet window.setStyleSheet(""" QComboBox:!editable { border: 5px solid red; } QComboBox:editable { border: 2px solid blue; } """) window.setLayout(layout) window.show() sys.exit(app.exec_()) 

In the above code:

  1. A non-editable QComboBox (named combo) and an editable QComboBox (named combo2) are created.
  2. Using the stylesheet, a border of 5 pixels is set for non-editable QComboBox (represented by QComboBox:!editable) and a border of 2 pixels for editable QComboBox (represented by QComboBox:editable).

When you run the above example, you should see the non-editable combo box with a thicker red border and the editable combo box with a thinner blue border. Adjust the styles and values as needed to achieve your desired appearance.


More Tags

getderivedstatefromprops actionlistener aws-sdk-android jdbc-odbc windows-1252 imshow webhooks dynamic-rdlc-generation program-entry-point mapfragment

More Programming Guides

Other Guides

More Programming Examples