PyQt5可选中的组合框:显示已选项目列表。

3
基于https://dev59.com/sH7aa4cB1Zd3GeqPnTna#22775990 可选复选框实现,我想进一步能够直接在主要的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_())

一张解释我想要的图片: 图表


你说的“So far, I am able to print the list of the checked items, but I don't have a clue how to change the main QComboBox label text with the former”是什么意思?当你从下拉菜单中勾选框时,列表顺序是否会在外观上发生变化?如果更容易理解,你可以画一张图片并将其添加到你的问题中。 - ZF007
感谢您的反馈:我上传了一张解释性图片。希望现在更清楚了 :) - rubebop
@rubebop,我觉得将另一个问题标记为正确答案有些矛盾,也许我误解了你的问题。从我的理解来看,QComboBox标签将显示所选项目的文本,这就是我的解决方案与其他解决方案不同之处,后者每次选择或取消选择项目时都会创建一个新项目,想象一下如果你有1000个选项,那么你将有许多不可选的选项而不是少数可选的选项。 - eyllanesc
@ZF007请不要添加不必要的标签,这里Python3与之无关。 - eyllanesc
@eyllanesc 这只是一个复制/粘贴错误的问题。现在已经修复了。我添加了py3.x,因为print语句的构造显示它是py3.x。没什么大不了的。 - ZF007
这将有助于解决您的问题。 - Kumar
2个回答

6

您可以重载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)

1
在我正在实现的程序中,这个解决方案存在一个大问题。每次运行check_items函数时,我都会记录checkItems列表。使用您的解决方案,只要将光标放在CheckableComboBox上,该函数就会自动运行,从而创建非期望的日志。如果不是通过handle_item_pressed函数触发“pressed”操作的QPushButton,我不想运行check_items。 - rubebop

1

通过对原始代码进行微小的更改,您可以轻松实现以下代码。当您打开comboBox选项卡时,您将看到所有项目标签都已更新为当前选定的选项。

示例:

  1. 如果选择了所有选项,则会得到以下结果:

"Combobox Item 0 - 选定的项目: 0、1、2"

或者

  1. 例如,如果删除了选项0:

Combobox Item 2 - 选定的项目: 1、2

代码:

    def check_items(self):
        checkedItems = []
        for i in range(self.count()):
            if self.item_checked(i):
                checkedItems.append(i)

        self.update_labels(checkedItems)

    def update_labels(self, item_list):

        n = ''
        count = 0
        for i in item_list:
            if count == 0:
                n += ' %s' % i
            else:
                n += ', %s' % i

            count += 1

#        print('n : "%s".' % n)

        for i in range(self.count()):

            text_label = self.model().item(i, 0).text()

#            print('Current (index %s) text_label "%s"' % (i, text_label))
#            sys.stdout.flush()

            if text_label.find('-') >= 0:
                text_label = text_label.split('-')[0]

            item_new_text_label = text_label + ' - selected item(s): ' + n

#            self.model().item(i).setText(item_new_text_label)
             self.setItemText(i, item_new_text_label)  # copy/paste error corrected.

#        print(item_list)
        sys.stdout.flush()

我认为可以删除 "text_label.rstrip(' %s' % i)",因为输出未被使用 ;) - rubebop
关于这一点,您是正确的,可以毫不问题地将其删除或使用带有解释注释的#。这有助于更快地获得“CopyEditor”徽章;-) - ZF007
谢谢你的提示:我已经删除了那行^^ - rubebop
@ZF007,下拉框的选项名称也更改了,我认为这不是一个好选择。用户可能会在选择时感到困惑。 - Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接