PyQt5 QComboBox - Change border style when it is in off state and when it get pressed
Last Updated : 17 May, 2020
In this article we will see how we can change the border style of the combo box when it is in off state and get pressed, border style can be dotted, dashed etc. When we set border to the combo box it is continuous although we can change it. Styled border will be only visible when combo box is in off state and it gets pressed else normal border will be visible.
In order to do this we have to change the style sheet associated with the combo box, below is the style sheet code
# importing librariesfromPyQt5.QtWidgetsimport*fromPyQt5importQtCore,QtGuifromPyQt5.QtGuiimport*fromPyQt5.QtCoreimport*importsysclassWindow(QMainWindow):def__init__(self):super().__init__()# setting titleself.setWindowTitle("Python ")# setting geometryself.setGeometry(100,100,600,400)# calling methodself.UiComponents()# showing all the widgetsself.show()# method for widgetsdefUiComponents(self):# creating a check-able combo box objectself.combo_box=QComboBox(self)# setting geometry of combo boxself.combo_box.setGeometry(200,150,100,40)# making combo box editable# self.combo_box.setEditable(True)# geek listgeek_list=["Sayian","Super Sayian","Super Sayian 2","Super Sayian B"]# adding list of items to combo boxself.combo_box.addItems(geek_list)# setting stylesheet of the combo box# set the border style when it is in off state# and get pressedself.combo_box.setStyleSheet("QComboBox""{""border : 4px solid black""}""QComboBox::! on:pressed""{""border : 4px black;""border-style : dotted;""}")# create pyqt5 appApp=QApplication(sys.argv)# create the instance of our Windowwindow=Window()# start the appsys.exit(App.exec())