[PySide] QCombobox : Signal not emitted

Cristián Maureira-Fredes Cristian.Maureira-Fredes at qt.io
Tue Apr 10 09:09:31 CEST 2018


Hello Serge,

After executing your script, I get the following output:
https://pastebin.com/2L3VYR0n
I do get the output from `selectionchange` and `highlighted`,
is this not the desire behavior?

Cheers

________________________________________
From: PySide <pyside-bounces+cristian.maureira-fredes=qt.io at qt-project.org> on behalf of Serge K. <skab12 at gmail.com>
Sent: 09 April 2018 22:46:32
To: pyside at qt-project.org
Subject: [PySide] QCombobox : Signal not emitted

Hi Everyone,

After setting a custom model on QCombobox, I do not receive anymore currentIndexChanged signal from my QCombobox (look below as an example) Did I do something wrong?


from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *


class CustomModel(QAbstractListModel):
    """ Protocol model """

    def __init__(self, *args, **kwargs):
        super(CustomModel, self).__init__(*args, **kwargs)

        self._protocol_list = []

    def add_protocol(self, data, row=0):
        if not row:
            row = len(self._protocol_list)

        self.beginInsertRows(QModelIndex(), row, row)
        self._protocol_list.insert(row, data)
        self.endInsertRows()
        return self.index(len(self._protocol_list) - 1)

    def remove_protocol(self, item):
        for row, data in enumerate(self._protocol_list):
            if data.name.lower() == item.name.lower():
                self.beginRemoveRows(QModelIndex(), row, row)
                self._protocol_list.pop(row)
                self.endRemoveRows()

    def rowCount(self, parent=QModelIndex()):
        if parent.isValid():
            return 0
        return len(self._protocol_list)


    def insertRows(self, row, count, index=QModelIndex()):
        self.beginInsertRows(QModelIndex(), row, row + count - 1)

        for j in range(row, row + count):
            self._protocol_list.insert(j, None)

        self.endInsertRows()
        return True

    def removeRows(self, row, count, index=QModelIndex()):
        self.beginRemoveRows(QModelIndex(), row, row + count - 1)

        for j in range(row, row + count)[::-1]:
            self._protocol_list.pop(j)

        self.endRemoveRows()
        return True

    def headerData(self, section, orientation, role=Qt.EditRole):
        return None

    def data(self, index, role=Qt.DisplayRole):
        if not index.isValid():
            return None

        current_protocol = self._protocol_list[index.row()]

        if role == Qt.DisplayRole or role == Qt.EditRole:
            return current_protocol[0]

        return None

    def setData(self, index, value, role=Qt.EditRole):
        if role != Qt.EditRole:
            return True

        if index.isValid() and 0 <= index.row() < len(self._protocol_list):
            current_protocol = self._protocol_list[index.row()]
            if index.column() == 0:
                current_protocol[0] =  value

                self.dataChanged.emit(index, index)
                return True

        return False

    def flags(self, index):
        if index.isValid():
            return Qt.ItemFlags(Qt.ItemIsEnabled | Qt.ItemIsEditable)
        return QAbstractListModel.flags(index)


class combodemo(QWidget):
    def __init__(self, parent=None):
        super(combodemo, self).__init__(parent)

        layout = QHBoxLayout()
        self.cb = QComboBox()
        self.model = CustomModel()
        self.cb.setModel(self.model)
        self.cb.setModelColumn(0)
        self.model.add_protocol(("c", "A"))
        self.model.add_protocol(("c++", "B"))
        self.model.add_protocol(("Java", "C"))
        self.model.add_protocol(("C#", "D"))
        self.model.add_protocol(("Python", "E"))

        self.cb.currentIndexChanged.connect(self.selectionchange)
        self.cb.highlighted.connect(self.highlighted)

        layout.addWidget(self.cb)
        self.setLayout(layout)
        self.setWindowTitle("combo box demo")

    def highlighted(self, i):
        print("Highlighted: ", i)

    def selectionchange(self, i):
        print("Items in the list are :")

        for count in range(self.cb.count()):
            print(self.cb.itemText(count))
        print("Current index", i, "selection changed ", self.cb.currentText())



def main():
    app = QApplication(sys.argv)
    ex = combodemo()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()




More information about the PySide mailing list