Python 更改组合框时更新QAbstractTableModel

Python 更改组合框时更新QAbstractTableModel,python,pyside,qcombobox,qabstracttablemodel,qitemdelegate,Python,Pyside,Qcombobox,Qabstracttablemodel,Qitemdelegate,当组合框更改时,我正在尝试更新我的模型。我有一个连接到combobox的函数,它更新模型,但来自“代理应该在提供标准行为(访问index.model()。_数据不好)时使用模型的基本函数”。我还希望组合框的更改能够影响QLineEdit中显示的内容 Qitem代表: class Delegate(QItemDelegate): def __init__(self): QItemDelegate.__init__(self) self.type_items

当组合框更改时,我正在尝试更新我的模型。我有一个连接到combobox的函数,它更新模型,但来自“代理应该在提供标准行为(访问index.model()。_数据不好)时使用模型的基本函数”。我还希望组合框的更改能够影响QLineEdit中显示的内容

Qitem代表:

class Delegate(QItemDelegate):

    def __init__(self):
        QItemDelegate.__init__(self)
        self.type_items = ["1", "2"]

    def createEditor(self, parent, option, index):
        if index.column() == 0:
            comboBox = QComboBox(parent)
            for text in self.type_items:
                comboBox.addItem(text, (index.row(), index.column()))
                comboBox.currentIndexChanged.connect(lambda: self.comboBoxChanged(comboBox, index))
            return comboBox
        return super().createEditor(parent, option, index)

    # updates model but does not update view - shows what I want to happen
    def comboBoxChanged(self, comboBox, index):
        if comboBox.currentText() == "1":
            index.model()._data[index.row()][0] = "1"
            index.model()._data[index.row()][1] = "One"
        elif comboBox.currentText() == "2":
            index.model()._data[index.row()][0] = "2"
            index.model()._data[index.row()][1] = "Two"
        # self.dataChanged.emit(index, index) # can't call this here
QAbstractTableModel:

class TableModel(QAbstractTableModel):
    def __init__(self, data):
        super(TableModel, self).__init__()
        self._data = data

    def data(self, index, role):
        if role in (Qt.DisplayRole, Qt.EditRole):
            return self._data[index.row()][index.column()]

    def rowCount(self, index=None):
        return len(self._data)

    def columnCount(self, index=None):
        return len(self._data[0])

    def flags(self, index):
        return super().flags(index) | Qt.ItemIsEditable

    def setData(self, index, value, role=Qt.EditRole):
        if role == Qt.EditRole:
            self._data[index.row()][index.column()] = value
            self.dataChanged.emit(index, index)
            return True
        return False
主要内容:

  • 如何在qcombobox中注册更改以正确更新模型? 及
  • 如何使其触发qlineedit中的更改

  • 最好使用现有的
    setData
    功能更新模型的数据,该功能提供一个集中的界面,避免混淆错误和错误

    在您的情况下,您需要设置同级索引的数据,这是下一列中同一行的索引(无需设置索引值本身,因为Qt能够从组合中自动获取它,并且无论如何都应该在
    setModelData()
    中完成)

    需要明确的是,您的代码本来可以工作,但是
    dataChanged
    是模型的信号,而不是代理的信号,因此它应该是:

            index.model().dataChanged.emit(index, index)
    

    但是,如前所述,最好避免使用不同的方法来更改模型中的数据,尤其是在从外部更改数据时。使用一个函数来处理这个问题的主要好处是,每当模型的结构和实现发生变化时,唯一需要检查的就是该函数调用的所有发生情况(例如,在列顺序发生变化的情况下).

    为什么您仍然试图以这种方式设置数据,而不是像前面解释的那样使用
    setData()
    ?我包含该函数是为了显示我想要做的事情,但我知道这是错误的方式,我要求的是正确的方式。setData()和data()都不是当我更改组合框的当前索引时调用,除非我在更改它们之前选择了组合框下面的单元格。您应该在comboBoxChanged函数中调用
    setData
        def comboBoxChanged(self, comboBox, index):
            if comboBox.currentText() == "1":
                value = "One"
            elif comboBox.currentText() == "2":
                value = "Two"
            sibling = index.sibling(index.row(), 1)
            index.model().setData(sibling, value)
    
            index.model().dataChanged.emit(index, index)