效果:
源码:
from PySide6.QtWidgets import QComboBox, QListWidget, QCheckBox, QListWidgetItem, QLineEdit, QApplication, QHBoxLayout, \
from PySide6.QtGui import Qt
from PySide6.QtCore import Signal
class XCombobox(QComboBox):
itemChecked = Signal(list)
def __init__(self, parent) -> None:
listwgt = QListWidget(self)
self.setModel(listwgt.model())
lineEdit = QLineEdit(self)
lineEdit.setReadOnly(True)
self.setLineEdit(lineEdit)
def add_item(self, text: str):
check = QCheckBox(text, self.view())
check.stateChanged.connect(self.on_state_changed)
self._checks.append(check)
item = QListWidgetItem(self.view())
item.setFlags(item.flags() & Qt.IntersectsItemShape)
self.view().addItem(item)
self.view().setItemWidget(item, check)
def add_items(self, texts: list):
if self._checks[0] == chk:
if chk.checkState() == Qt.Checked:
sel_data.append(chk.text())
def set_all_state(self, state):
chk.setCheckState(Qt.CheckState(state))
def on_state_changed(self, state):
if self.sender() == self._checks[0]:
self.set_all_state(state)
sel_data = self.get_selected()
self.itemChecked.emit(sel_data)
self.lineEdit().setText(';'.join(sel_data))
if __name__ == '__main__':
cmbox.add_items(['AAA', 'BBB', 'CCC', 'DDD','AAA', 'BBB', 'CCC', 'DDD','AAA', 'BBB', 'CCC', 'DDD','AAA', 'BBB', 'CCC', 'DDD','AAA', 'BBB', 'CCC', 'DDD','AAA', 'BBB', 'CCC', 'DDD','AAA', 'BBB', 'CCC', 'DDD','AAA', 'BBB', 'CCC', '11DDD'])
hyout = QHBoxLayout(wind)