33 lines
1.0 KiB
Python
33 lines
1.0 KiB
Python
from PyQt5.QtWidgets import QComboBox
|
|
from PyQt5.QtCore import Qt
|
|
|
|
|
|
class CheckableComboBox(QComboBox):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self._changed = False
|
|
self.view().pressed.connect(self.handleItemPressed)
|
|
|
|
def setItemChecked(self, index, checked=False):
|
|
item = self.model().item(index, self.modelColumn()) # QStandardItem object
|
|
if checked:
|
|
item.setCheckState(Qt.Checked)
|
|
else:
|
|
item.setCheckState(Qt.Unchecked)
|
|
|
|
def handleItemPressed(self, index):
|
|
item = self.model().itemFromIndex(index)
|
|
if item.checkState() == Qt.Checked:
|
|
item.setCheckState(Qt.Unchecked)
|
|
else:
|
|
item.setCheckState(Qt.Checked)
|
|
self._changed = True
|
|
|
|
def hidePopup(self):
|
|
if not self._changed:
|
|
super().hidePopup()
|
|
self._changed = False
|
|
|
|
def itemChecked(self, index):
|
|
item = self.model().item(index, self.modelColumn())
|
|
return item.checkState() == Qt.Checked |