Open In App
Related Articles

PyQt5 Checkable ComboBox – Showing checked items in textview

Improve Article
Save Article
Like Article

In this article we will see how we can show the checked items in the text view(line edit) part of the combo box, we know in order to create the checkable items in the combo box we have to create a class of check-able combo box which inherits the combo box class and add the check-able items, but when we check items only the last item user check is visible in the text view part not all the check items are shown.

In order to show the selected items in the text view part, do the following – 1. Inside the check-able combo box class 2. Add function which get called when any item get check 3. Inside that function check which items are checked by iterating all the items 4. Show indexes of all the item to the combo box which are checked

Below is the implementation 


from PyQt5.QtWidgets import QApplication, QComboBox, QMainWindow
from PyQt5.QtWidgets import QWidget, QVBoxLayout
from PyQt5.QtGui import QStandardItemModel
from PyQt5.QtCore import Qt
import sys
# creating checkable combo box class
class CheckableComboBox(QComboBox):
    def __init__(self):
        super(CheckableComboBox, self).__init__()
    # when any item get pressed
    def handle_item_pressed(self, index):
        # getting which item is pressed
        item = self.model().itemFromIndex(index)
        # make it check if unchecked and vice-versa
        if item.checkState() == Qt.Checked:
        # calling method
    # method called by check_items
    def item_checked(self, index):
        # getting item at index
        item = self.model().item(index, 0)
        # return true if checked else false
        return item.checkState() == Qt.Checked
    # calling method
    def check_items(self):
        # blank list
        checkedItems = []
        # traversing the items
        for i in range(self.count()):
            # if item is checked add it to the list
            if self.item_checked(i):
        # call this method
    # method to update the label
    def update_labels(self, item_list):
        n = ''
        count = 0
        # traversing the list
        for i in item_list:
            # if count value is 0 don't add comma
            if count == 0:
                n += ' % s' % i
            # else value is greater than 0
            # add comma
                n += ', % s' % i
            # increment count
            count += 1
        # loop
        for i in range(self.count()):
            # getting label
            text_label = self.model().item(i, 0).text()
            # default state
            if text_label.find('-') >= 0:
                text_label = text_label.split('-')[0]
            # shows the selected items
            item_new_text_label = text_label + ' - selected index: ' + n
           # setting text to combo box
            self.setItemText(i, item_new_text_label)
    # flush
class Window(QMainWindow):
    def __init__(self):
        super(QMainWindow, self).__init__()
        # creating a widget object
        myQWidget = QWidget()
        # vertical box layout
        myBoxLayout = QVBoxLayout()
        # central widget
        # creating checkable combo box
        self.ComboBox = CheckableComboBox()
        # traversing items
        for i in range(3):
            # adding item
            self.ComboBox.addItem("Combobox Item " + str(i))
            item = self.ComboBox.model().item(i, 0)
            # setting item unchecked
        # adding combo box to the layout
# drivers code
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.resize(480, 320)

Output :

Last Updated : 06 Jan, 2023
Like Article
Save Article
Similar Reads