PyQt5 Combo Box - Different border size when non-editable and in off state

PyQt5 Combo Box - Different border size when non-editable and in off state

To customize the border size of a QComboBox in PyQt5 when it is non-editable and in the "OFF" state (i.e., not focused or not active), you can utilize Qt's stylesheet feature.

Below is an example demonstrating how to set a different border size for a non-editable QComboBox based on its focus state:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox class AppDemo(QWidget): def __init__(self): super().__init__() layout = QVBoxLayout(self) comboBox = QComboBox(self) comboBox.addItems(['Option 1', 'Option 2', 'Option 3']) comboBox.setEditable(False) # Make ComboBox non-editable # Apply stylesheet comboBox.setStyleSheet(""" QComboBox:editable { border: 3px solid green; # This won't apply since the ComboBox is non-editable } QComboBox:!editable { border: 2px solid gray; # Border for non-editable ComboBox } QComboBox:!editable:focus { border: 4px solid blue; # Border for non-editable ComboBox when focused } """) layout.addWidget(comboBox) app = QApplication(sys.argv) demo = AppDemo() demo.show() sys.exit(app.exec_()) 

In the above example:

  • A non-editable QComboBox has a gray border with a width of 2 pixels.
  • When the non-editable QComboBox receives focus, its border becomes thicker (4 pixels) and turns blue.

The stylesheet properties ensure that the styles only apply to the non-editable state of the QComboBox. Adjust the border sizes and colors in the stylesheet to fit your specific requirements.


More Tags

airflow selector tfs model-view-controller mule-el asp.net-core-2.2 git-rewrite-history gateway output azure-storage-files

More Programming Guides

Other Guides

More Programming Examples