0

I am trying to update my QTableView after I receive a notice via pydispatcher of a change in the system. I did create the following functions

def rowCount(self, parent=None):
    return len(self.m_list)

def columnCount(self, parent=None):
    return len(self.table_def)

def headerData(self, col, orientation, role):
    if orientation == Qt.Horizontal and role == Qt.DisplayRole:
        return self.table_def[col]['Header']
    return QVariant()

def data(self, index, role=Qt.DisplayRole):
    if not index.isValid():
        return None
    if index.row() >= len(self.m_list) or index.row() < 0:
        return None

    row = index.row()
    col = index.column()

    print("Called for (%s, %s, %s)" % (row, col, role))
    if role == Qt.DisplayRole:
        return self.table_def[index.column()]['Function'](index.row())
    elif role == Qt.BackgroundRole:
        batch = (index.row() // 100) % 2
        if batch == 0:
            return QApplication.palette().base()

        return QApplication.palette().alternateBase()
    else:
        return None

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

def update_model(self, data):
    print('update_model')
    index_1 = self.index(0, 0)
    index_2 = self.index(0, 1)
    self.dataChanged.emit(index_1, index_2, [Qt.DisplayRole])

The line self.dataChanged.emit(index_1, index_2, [Qt.DisplayRole]) does not seems to do anything; i.e. data(self, index, role=Qt.DisplayRole) is not called.

If I click on the table, then data(self, index, role=Qt.DisplayRole) is called and the table update.

The fix that I have right now is to call beginResetModel() and endResetModel(). That works, but it is not how it should work.

Any idea what could be happening?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
PBareil
  • 157
  • 6
  • 1
    If you want us to help you, you must provide a [mcve] – eyllanesc Feb 28 '18 at 16:56
  • This should work. Try `QtCore.Qt.EditRole` but that shouldn't make a difference. My guess is that there is a bug elsewhere which swallows the update. Try to connect to the signal and print when it's emitted. – Aaron Digulla Apr 23 '20 at 16:15

1 Answers1

1

I had the same problem and I fixed it just by calling self.headerDataChanged.emit instead. So, to do that, once you change something in the table, call the following:

self.headerDataChanged.emit(Qt.Horizontal, idx1, idx2)

where self._data is your data within the class. idx1 and idx2 are the first and last indexes of changed data, respectively. Qt.Horizontal is an example and it could be vertical depending on your table content.

eleaut
  • 11
  • 3