1

I am trying to make a QComboBox with checkable items. I created a custom Model, which my QComboBox object uses (via the setModel() method).

I tried using the solution presented in this question : https://stackoverflow.com/a/8423904

Here is my custom model :

class FilterModel(QStandardItemModel):

    def __init__(self, filter_list, parent=None):
        super(FilterModel, self).__init__(parent)

        for index, filter in enumerate(filter_list):
            item = QStandardItem(filter)
            item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
            item.setData(Qt.Unchecked, Qt.CheckStateRole)

            self.setItem(index, 0, item)

I pass the items as a list in the constructor when I instantiate my model.

However, my items are nor selectable, nor checkable (the checkbox isn't even displayed).

Community
  • 1
  • 1
Klmnop
  • 15
  • 5
  • Yes, as said in my question, that's the answer I used to implement this. I copy/pasted his code, transformed it from C++ to Python, but here I am, it does not work (items are nor selectable, nor checkable). – Klmnop May 18 '16 at 12:35

1 Answers1

0

I don't know why the checkbox doesn't show but for the item to be selectable you'll also need to include the Qt.ItemIsSelectable flag.

In any case, why don't you try the QStandardItem.setCheckable method? Something like this:

class FilterModel(QStandardItemModel):

    def __init__(self, filter_list, parent=None):
        super(FilterModel, self).__init__(parent)

        for index, filter in enumerate(filter_list):
            item = QStandardItem(filter)
            item.setSelectable(True)
            item.setCheckable(True)
            item.setCheckState(Qt.Unchecked)
            self.setItem(index, 0, item)
titusjan
  • 5,376
  • 2
  • 24
  • 43