[英]PyQt5 checkable combo box: display list of checked items
基於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_())
您可以覆蓋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)
對原始代碼稍作改動,您就可以輕松實現以下代碼。 當您打開組合框選項卡時,您將看到所有項目標簽都更新為當前選定的選項。
例子:
“組合框項目 0 - 所選項目:0、1、2”
或者
組合框項目 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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.