Select item in custom QComboBox not working

28 Views Asked by At

I have a custom QComboBox with a TreeView to split child item in different sections, the parent items are not selectable. When the user triggers it works fine, but when i try to automatically select one of the item, it seems like my comboBox only sees the parent items. Maybe someone could help.

Here is my custom comboBox, (selectChildItem will only select a parent item, findText of a childItem will return -1). The index of the currentIndexChanged event when the user triggers and select a child item is 0

class CustomComboBox(QtWidgets.QComboBox):

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

        self.view = QtWidgets.QTreeView()
        self.view.setHeaderHidden(True)

        self.model = QtGui.QStandardItemModel()

        self.setModel(self.model)
        self.setView(self.view)

    def addUserItems(self, header, items):
        userItem = QtGui.QStandardItem(header)
        userItem.setSelectable(False)
        for item in sorted(items):
            childItem = QtGui.QStandardItem(item)
            userItem.appendRow(childItem)
        self.model.appendRow(userItem)

        self.view.expandAll()
        self.view.setItemsExpandable(False)

    # This does not select the child item
    def selectChildItem(self, itemText):
        for row in range(self.model.rowCount()):
            headerItem = self.model.item(row)
            for col in range(headerItem.rowCount()):
                childItem = headerItem.child(col)
                if childItem.text() == itemText:
                    self.view.selectionMode()
                    index = self.model.indexFromItem(childItem)
                    self.setCurrentIndex(index.row())

0

There are 0 best solutions below