Попробуйте так:
import sys
from PyQt5.Qt import *
LastStateRole = Qt.UserRole + 1000
def checkable_combox(combo, options):
model = QStandardItemModel(len(options), 1)
for i, area in enumerate(options):
item = QStandardItem(area)
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
item.setData(Qt.Unchecked, Qt.CheckStateRole)
model.setItem(i, 0, item)
combo.setModel(model)
class Example(QWidget):
def __init__(self):
super().__init__()
self.label = QLabel()
self.combo = QComboBox(self)
usecase_type = ["сделать выбор", "option 1", "option 2", "option 3"]
checkable_combox(self.combo, usecase_type)
self.combo.model().itemChanged.connect(self.on_itemChanged)
self.btn = QPushButton("Button")
self.btn.clicked.connect(self.btn_clicked)
self.layout = QVBoxLayout(self)
self.layout.addWidget(self.label)
self.layout.addWidget(self.combo)
self.layout.addWidget(self.btn)
def btn_clicked(self):
self.label.clear()
for i in range(self.combo.model().rowCount()):
item = self.combo.model().item(i, 0)
checked = item.data(Qt.CheckStateRole)
if checked and (item.text() != "сделать выбор"):
self.label.setText(f'{self.label.text()}\n{item.text()}: checked')
@pyqtSlot("QStandardItem*")
def on_itemChanged(self, item):
last_state = item.data(LastStateRole)
current_state = item.data(Qt.CheckStateRole)
if last_state != current_state:
item.model().blockSignals(True)
item.setData(current_state, LastStateRole)
item.model().blockSignals(False)
#print(f'{item.text()}: {current_state}' )
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setFont(QFont("Times", 12, QFont.Bold))
ex = Example()
ex.resize(300, 300)
ex.show()
sys.exit(app.exec_())