Issue
I need my combobox items to be checkable by user so they can select multiple items. I've seen some solution on stackoverflow. I applied it to my code. It did something, but checkbox aren't visible yet and I'm not yet able to select any item.
My code is as follows
from PySide.QtGui import *
from PySide.QtCore import *
udims = [here goes some items]
cmb = QComboBox()
model = QStandardItemModel(len(udims), 1)
for i, udim in enumerate(udims):
item = QStandardItem(udim)
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
item.setData(Qt.Checked | Qt.CheckStateRole)
model.setItem(i, 0, item)
cmb.setModel(model)
Solution
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.
from PySide.QtCore import *
from PySide.QtGui import *
class CheckBoxDelegate(QItemDelegate):
def __init__(self, parent=None):
super(CheckBoxDelegate, self).__init__(parent)
def createEditor(parent, op, idx):
self.editor = QCheckBox(parent)
udims = ['1001', '1002', '1003', '1004']
cmb = QComboBox()
delegate = CheckBoxDelegate()
cmb.setItemDelegate(delegate)
model = QStandardItemModel(len(udims), 1)
for i, udim in enumerate(udims):
item = QStandardItem(udim)
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
item.setData(Qt.Checked | Qt.CheckStateRole)
model.setItem(i, 0, item)
cmb.setModel(model)
Answered By - Vicspidy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.