PyQt5 Checkable ComboBox - Showing checked items in textview

PyQt5 Checkable ComboBox - Showing checked items in textview

In PyQt5, a checkable combo box doesn't come out of the box, so you have to create your own by subclassing QComboBox and implementing the checkable functionality. Here's an example of how you can create a QComboBox where items can be checked or unchecked, and the selected items are shown in a QLineEdit as a comma-separated list:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QComboBox, QVBoxLayout, QLineEdit, QStandardItemModel, QStandardItem
from PyQt5.QtCore import Qt

class CheckableComboBox(QComboBox):
    def __init__(self):
        super(CheckableComboBox, self).__init__()
        self.setView(QStandardItemModel())
        self.setEditable(True)
        self.lineEdit().setReadOnly(True)
        self.lineEdit().setPlaceholderText("Select options")

    def addItem(self, text, data=None):
        item = QStandardItem(text)
        item.setData(data)
        item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsUserCheckable)
        item.setData(Qt.Unchecked, Qt.CheckStateRole)
        self.model().appendRow(item)

    def itemChecked(self, index):
        item = self.model().item(index, 0)
        return item.checkState() == Qt.Checked

    def checkItem(self, index):
        item = self.model().item(index, 0)
        item.setCheckState(Qt.Checked if item.checkState() == Qt.Unchecked else Qt.Unchecked)

    def hidePopup(self):
        super(CheckableComboBox, self).hidePopup()
        selected_items = []
        for i in range(self.model().rowCount()):
            if self.itemChecked(i):
                selected_items.append(self.model().item(i).text())
        self.lineEdit().setText(", ".join(selected_items))

    def mousePressEvent(self, event):
        self.showPopup()

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.title = 'Checkable ComboBox Example'
        self.initUI()
    
    def initUI(self):
        self.setWindowTitle(self.title)
        layout = QVBoxLayout()

        # Create checkable combo box
        self.comboBox = CheckableComboBox()
        # Add items to the combo box
        for i in range(10):
            self.comboBox.addItem(f'Item {i+1}')

        layout.addWidget(self.comboBox)

        # Line edit to show selected items
        self.lineEdit = QLineEdit(self)
        self.lineEdit.setReadOnly(True)
        layout.addWidget(self.lineEdit)

        # Update line edit text when combo box selection changes
        self.comboBox.lineEdit().textChanged.connect(self.updateLineEdit)

        self.setLayout(layout)
        self.show()
    
    def updateLineEdit(self, text):
        self.lineEdit.setText(text)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

In this code:

  • We create a CheckableComboBox class derived from QComboBox.
  • We use QStandardItemModel and QStandardItem to add checkable items to the combo box.
  • We override the hidePopup() method to update the QLineEdit with the selected items whenever the popup is closed.
  • We implement a custom mousePressEvent to show the popup. You can modify it if you want to perform other actions when the mouse is pressed.

When you run the application, you'll see a combo box with checkable items. Selecting or deselecting these items will update the text of the QLineEdit widget.


More Tags

formatted-input delete-file flat maven-ant-tasks git radix countif android-backup-service uikit ios11

More Programming Guides

Other Guides

More Programming Examples