python - PyQt5可检查组合框: display list of checked items

标签 python pyqt pyqt5 qcombobox

基于https://stackoverflow.com/a/22775990/7894940可检查组合框实现,我想更进一步,能够直接在主 QComboBox 标签上显示选中项目的列表,即当 QComboBox 的显示文本不是“展开”时.

到目前为止,我可以打印选中项目的列表,但我不知道如何使用前者更改主 QComboBox 标签文本:

from PyQt5.QtWidgets import QApplication, QComboBox, QMainWindow, QWidget, QVBoxLayout
from PyQt5.QtGui import QStandardItemModel
from PyQt5.QtCore import Qt
import sys


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

    def handle_item_pressed(self, index):
        item = self.model().itemFromIndex(index)
        if item.checkState() == Qt.Checked:
            item.setCheckState(Qt.Unchecked)
            # print(item.text() + " was unselected.")
        else:
            item.setCheckState(Qt.Checked)
            # print(item.text() + " was selected.")
        self.check_items()

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

    def check_items(self):
        checkedItems = []
        for i in range(self.count()):
            if self.item_checked(i):
                checkedItems.append(self.model().item(i, 0).text())
        print(checkedItems)


class Dialog_01(QMainWindow):
    def __init__(self):
        super(QMainWindow, self).__init__()
        myQWidget = QWidget()
        myBoxLayout = QVBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)
        self.ComboBox = CheckableComboBox()
        for i in range(3):
            self.ComboBox.addItem("Combobox Item " + str(i))
            item = self.ComboBox.model().item(i, 0)
            item.setCheckState(Qt.Unchecked)
        myBoxLayout.addWidget(self.ComboBox)

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

一张解释我想要什么的图片: Diagram

最佳答案

您可以重写paintEvent方法:

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

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

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

    def check_items(self):
        checkedItems = []
        for i in range(self.count()):
            if self.item_checked(i):
                checkedItems.append(self.model().item(i, 0).text())
        return checkedItems

    def paintEvent(self, event):
        painter = QStylePainter(self)
        painter.setPen(self.palette().color(QPalette.Text))
        opt = QStyleOptionComboBox()
        self.initStyleOption(opt)
        opt.currentText = ",".join(self.check_items())
        painter.drawComplexControl(QStyle.CC_ComboBox, opt)
        painter.drawControl(QStyle.CE_ComboBoxLabel, opt)

关于python - PyQt5可检查组合框: display list of checked items,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59767605/

相关文章:

Python eval 在函数内部不起作用

python - 使用下拉菜单外键 : IntegrityError XXX_id may not be NULL 保存表单集

linux - 如何使用 PyQt5 QtMultimedia

python - PyQt5 QObject : Cannot create children for a parent that is in a different thread

python - QT 定时器不调用函数

python - 在 Python 中使用 lambda

python - 如何在 Python 中用零向右填充数字字符串?

python - 移动光标线位置 QTextEdit

python - 当在editingFinished事件中执行对话框时,QlineEdit的editingFinished被第二次发出

python - 如何将 PyQt 信号连接到外部函数