0

I'm using PYQT for developing an application. my requirement is to insert a tree view with checkbox inside a combobox items. I would like to know how to achieve this?

I have the following code but this does not work.

class CheckboxInsideListbox(QWidget):
def __init__(self, parent = None):
    super(CheckboxInsideListbox, self).__init__(parent)
    self.setGeometry(250,250,300,300)
    self.MainUI()

def MainUI(self):
    #stb_label = QLabel("Select STB\'s")
    stb_combobox = QComboBox()

    length = 10
    cb_layout = QVBoxLayout(stb_combobox)
    for i in range(length):
        c = QCheckBox("STB %i" % i)
        cb_layout.addWidget(c)

    main_layout = QVBoxLayout()
    main_layout.addWidget(stb_combobox)
    main_layout.addLayout(cb_layout)



    self.setLayout(main_layout)

Please let me know if I'm missing anything here.

user596922
  • 1,501
  • 3
  • 18
  • 27

2 Answers2

0

If you really mean to apply a layout to a layout, try adding your widget to your cb_layout. Otherwise get rid of your sublayout.

theheadofabroom
  • 20,639
  • 5
  • 33
  • 65
0

You should create a model that support Qt.CheckStateRole in data and SetData methods and the flag Qt.ItemIsUserCheckable in the flags method.

I Paste you here an example i am using in a project, this is a QSortFilterProxyModel generic implementation to use in any model but you can use the same ideas in your model implementation, obviously i am using internal structures in this subclass you have not directly in PyQt and are attached to my internal implementation (self.booleanSet and self.readOnlySet).

def flags(self, index):
    if not index.isValid():
        return Qt.ItemIsEnabled

    if index.column() in self.booleanSet:
        return Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled
    elif index.column() in self.readOnlySet:
        return Qt.ItemIsSelectable | Qt.ItemIsEnabled
    else:
        return QSortFilterProxyModel.flags(self, index)

def data(self, index, role):
    if not index.isValid():
        return QVariant()

    if index.column() in self.booleanSet and role in (Qt.CheckStateRole, Qt.DisplayRole):
        if role == Qt.CheckStateRole:
            value = QVariant(Qt.Checked) if index.data(Qt.EditRole).toBool() else QVariant(Qt.Unchecked)
            return value
        else: #if role == Qt.DisplayRole:
            return QVariant()
    else:
        return QSortFilterProxyModel.data(self, index, role)

def setData(self, index, data, role):
    if not index.isValid():
        return False

    if index.column() in self.booleanSet and role == Qt.CheckStateRole:
        value = QVariant(True) if data.toInt()[0] == Qt.Checked else QVariant(False)
        return QSortFilterProxyModel.setData(self, index, value, Qt.EditRole)
    else:
        return QSortFilterProxyModel.setData(self, index, data, role)
skuda
  • 1,068
  • 1
  • 6
  • 10