PyQt5 ComboBox - Different border size when editable and in off state

PyQt5 ComboBox - Different border size when editable and in off state

If you want to set a different border size for a QComboBox in PyQt5 based on its state (editable vs non-editable) and its "on/off" state (which typically means focused vs not-focused), you can achieve this using the QComboBox's stylesheet.

Here's a simple example to demonstrate how to set different border sizes for a QComboBox based on its editable state and focus state:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox class App(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() # Create two combo boxes: one editable and one non-editable combobox_editable = QComboBox(self) combobox_editable.addItems(['Item 1', 'Item 2', 'Item 3']) combobox_editable.setEditable(True) combobox_non_editable = QComboBox(self) combobox_non_editable.addItems(['Item A', 'Item B', 'Item C']) combobox_non_editable.setEditable(False) layout.addWidget(combobox_editable) layout.addWidget(combobox_non_editable) # Set styles stylesheet = """ QComboBox[editable="true"]:focus { border: 5px solid red; } QComboBox[editable="true"] { border: 3px solid green; } QComboBox { border: 2px solid blue; } """ self.setStyleSheet(stylesheet) self.setLayout(layout) self.setWindowTitle('PyQt5 ComboBox Border Styles') self.setGeometry(300, 300, 300, 150) self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_()) 

In this example:

  1. An editable QComboBox will have a 3px green border when it's not focused and a 5px red border when it's focused.
  2. A non-editable QComboBox will always have a 2px blue border, regardless of its focus state.

This is achieved by setting a custom stylesheet for the QComboBox widgets.


More Tags

azure-blob-storage azure-powershell webcam.js fingerprint git-filter-branch datetime-parsing uint8t ssim xssf undo

More Programming Guides

Other Guides

More Programming Examples