I want to populate a combobox with udims of current geometry and I also want to make it checkable.
I have applied a model to combobox as a data. Here’s my code
from PySide.QtGui import *
from PySide.QtCore import *
cmb = QComboBox()
udims = [udim.name() for udim in mari.current.geo().patchList() ]
model = StandardItemModel(len(udims), 1)
for i, udim in enumerate(udims):
item = QStandardItem(udim)
item.setFlags(Qt.ItemIsUserCheckable | ItemIsEnabled)
item.setData(Qt.Checked | Qt.CheckStateRole)
model.setItem(item)
cmb.setModel(model)
Behavior of combox is changed but no checkbox is visible nor the items are selectable. I can’t figure out what’s the problem.
I was able to get this working by creating a subclass of QItemDelegate for the checkbox, then plugging that into the item delegate for the combobox. I didn’t use geometry but instead some random uuids to illustrate the point. Tested in Mari 3.3v1.
# Built in
from uuid import uuid4
# Third party
from PySide.QtCore import Qt
from PySide.QtGui import (
QComboBox,
QMainWindow,
QVBoxLayout,
QPushButton,
QItemDelegate,
QStandardItem,
QStandardItemModel
)
# Custom
ROWS = 10
class CheckBoxDelegate(QItemDelegate):
""" Check box delegate """
def __init__(self, parent=None):
""" Initialization """
super(CheckBoxDelegate, self).__init__(parent)
def createEditor(parent, op, idx):
""" Creates the checkbox """
self.editor = QCheckBox(parent)
class MainWin(QDialog):
""" Main window """
def __init__(self, parent=None):
""" Initialization """
super(MainWin, self).__init__(parent)
self.setWindowTitle('Test Window')
self.main_layout = QVBoxLayout()
self.setLayout(self.main_layout)
self.item_model = QStandardItemModel(ROWS, 1)
self.combobox = QComboBox()
self.checkboxdel = CheckBoxDelegate()
self.combobox.setItemDelegate(self.checkboxdel)
self.insert_test_data()
self.combobox.setModel(self.item_model)
self.main_layout.addWidget(self.combobox)
self.test_button = QPushButton('Print checked items')
self.test_button.clicked.connect(self.test_clicked)
self.main_layout.addWidget(self.test_button)
def insert_test_data(self):
""" Inserts test uuids into a combobox widget. """
for i in range(0, ROWS):
item = QStandardItem(str(uuid4()))
item.setCheckable(True)
item.setSelectable(False)
self.item_model.setItem(i, 0, item)
def test_clicked(self):
""" Prints info on checked items on click. """
for i in range(0, ROWS):
item = self.item_model.item(i, 0)
print(item.text(), item.checkState())
if __name__ == '__main__':
win = MainWin()
win.show()