如何从多选 QComboBox 中获取选中的项目?

How do I get checked items from multiselect QComboBox?

我能够使用 QComboBox 和 QStandardItemModel 实现 multi-select,但是我无法使用 selectedItems() 或 checkedItems 获得 selected 项目() 选项给出错误消息显示为:

AttributeError: 'QComboBox' object has no attribute 'selectedItems'               
AttributeError: 'QComboBox' object has no attribute 'checkedItems'

有没有办法获取 QComboBox 的 selected 项目列表? 下面是我的代码的示例实现

class CheckableComboBox(QComboBox):
    def __init__(self):
        super(CheckableComboBox, self).__init__()
        self.view().pressed.connect(self.handleItemPressed)
        self.setModel(QtGui.QStandardItemModel(self))

    def handleItemPressed(self, index):
        item = self.model().itemFromIndex(index)
        if item.checkState() == QtCore.Qt.Checked:
            item.setCheckState(QtCore.Qt.Unchecked)
        else:
            item.setCheckState(QtCore.Qt.Checked)

class Dialog_01(QMainWindow):
    def __init__(self):
        super(QMainWindow,self).__init__()       
        years = ["2017", "2018", "2019", "2020"]
        self.comboBoxv = QComboBox(self)
        self.model = QtGui.QStandardItemModel(self)
        firstItem = QtGui.QStandardItem("Select  Years")
        firstItem.setBackground(QtGui.QBrush(QtGui.QColor(200, 200, 200)))
        firstItem.setSelectable(False)
        self.model.setItem(0, 0, firstItem)
        for i,area in enumerate(years):
            item = QtGui.QStandardItem(area)
            item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
            item.setData(Qt.Unchecked, Qt.CheckStateRole)
            self.model.setItem(i+1, 0, item)
        self.comboBoxv.setModel(self.model)        
        self.comboBoxv.installEventFilter(self)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())

checkedItems 方法不存在,因此出现该错误。解决方案是迭代模型并验证它是否被选中。

from PyQt5 import QtCore, QtGui, QtWidgets

class CheckableComboBox(QtWidgets.QComboBox):
    closedPopup = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(CheckableComboBox, self).__init__(parent)
        self.setView(QtWidgets.QListView(self))
        self.view().pressed.connect(self.handleItemPressed)
        self.setModel(QtGui.QStandardItemModel(self))

        firstItem = QtGui.QStandardItem("Select  Years")
        firstItem.setBackground(QtGui.QBrush(QtGui.QColor(200, 200, 200)))
        firstItem.setSelectable(False)
        self.model().appendRow(firstItem)

    @QtCore.pyqtSlot(QtCore.QModelIndex)
    def handleItemPressed(self, index):
        item = self.model().itemFromIndex(index)
        if item.checkState() == QtCore.Qt.Checked:
            item.setCheckState(QtCore.Qt.Unchecked)
        else:
            item.setCheckState(QtCore.Qt.Checked)

    def checkedItems(self):
        l = []
        for i in range(self.model().rowCount()):
            it = self.model().item(i)
            if it.checkState() == QtCore.Qt.Checked:
                l.append(it.text())
        return l

    def hidePopup(self):
        self.closedPopup.emit()
        super(CheckableComboBox, self).hidePopup()
        QtCore.QTimer.singleShot(0, lambda: self.setCurrentIndex(0))

class Dialog_01(QtWidgets.QMainWindow):
    def __init__(self):
        super(Dialog_01,self).__init__()       
        years = ["2017", "2018", "2019", "2020"]
        self.comboBoxv = CheckableComboBox()
        self.comboBoxv.closedPopup.connect(self.on_closedPopup)
        for i, area in enumerate(years):
            item = QtGui.QStandardItem(area)
            item.setCheckable(True)
            item.setCheckState(QtCore.Qt.Unchecked)
            self.comboBoxv.model().appendRow(item)

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        lay = QtWidgets.QVBoxLayout(central_widget)
        lay.addWidget(self.comboBoxv)
        lay.addStretch()

    @QtCore.pyqtSlot()
    def on_closedPopup(self):
        print(self.comboBoxv.checkedItems())

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())