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

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

To set a different border size for an editable QComboBox when it's in an "on" state in PyQt5, you'll once again use the setStyleSheet method.

The "on" state for a QComboBox typically refers to when the drop-down list is shown. So, if you want a different border size when the QComboBox is editable and its drop-down list is shown, you can use the following style:

combo.setStyleSheet(""" QComboBox:editable:on { border-width: 3px; } """) 

Here's a complete example:

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() combo = QComboBox(self) combo.addItems(['Option 1', 'Option 2', 'Option 3']) # Making the ComboBox editable combo.setEditable(True) # Setting a different border size for the editable, on state combo.setStyleSheet(""" QComboBox:editable:on { border-width: 3px; border-color: blue; } """) layout.addWidget(combo) self.setLayout(layout) self.setWindowTitle('PyQt5 ComboBox') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_()) 

In this example, when you click on the editable QComboBox to show its drop-down list (putting it in the "on" state), you'll see the border width change. Adjust the border-width and other properties in the style sheet as necessary.


More Tags

entity-framework-6 background-attachment numbers arguments wait azure-application-insights special-characters storing-information webcrypto-api index-error

More Programming Guides

Other Guides

More Programming Examples