PyQt5 ComboBox - Different border size when non-editable and get pressed

PyQt5 ComboBox - Different border size when non-editable and get pressed

To set different border sizes for a non-editable QComboBox in PyQt5 when it gets pressed, you can again utilize Qt's stylesheet mechanism.

Here's a step-by-step guide to achieve this:

  1. Import necessary modules.
  2. Create a simple window with a QComboBox.
  3. Set a stylesheet to the QComboBox to change its border size when it's pressed.
import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox class AppDemo(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QComboBox with Different Border Size when Pressed") self.setGeometry(100, 100, 300, 200) layout = QVBoxLayout() combo = QComboBox() combo.addItems(["Option 1", "Option 2", "Option 3"]) combo.setEditable(False) # Ensure the ComboBox is non-editable # Set the stylesheet combo.setStyleSheet(""" /* Default state */ QComboBox { border: 2px solid gray; } /* Pressed state */ QComboBox::down-arrow:on { /* if you only want to change the arrow */ /* Add style for the arrow when the ComboBox is pressed */ } QComboBox::drop-down:editable:on { /* Add style for the editable part of the ComboBox when pressed */ } QComboBox:on { /* This targets the entire ComboBox when it's pressed */ border: 5px solid blue; } """) layout.addWidget(combo) self.setLayout(layout) app = QApplication(sys.argv) demo = AppDemo() demo.show() sys.exit(app.exec_()) 

In this example, when the non-editable QComboBox is pressed, its border size changes from 2 pixels to 5 pixels, and the color changes to blue. You can modify these styles as per your requirements. Adjusting the stylesheets provides a lot of flexibility in customizing the visual appearance of PyQt5 widgets.


More Tags

visual-studio-debugging google-material-icons transactional poco double okhttp slick.js gradle zooming celery-task

More Programming Guides

Other Guides

More Programming Examples