0

I have a QTableView that populates files and folders as items using QAbstractTableModel, I didn't used QFileSystemModel because the table view doesnt solely displays folder names, it also displays attribute of files within the folder.

So my question is how do I add a validator on DisplayRole or if someone tries to edit the folder name in the EditRole, so that if the model loads the data and displays it should mark folder names with special character or those folder names with more than expected characters the cell containing the folder name should get red background ,

I have tried by adding a validator that raises exception , so if it is fine BackgroundColorRole returns QVariant else in case of exception it returns QtCore.Qt.red

Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197

1 Answers1

0

Maybe this will help:

class YourModel(QtCore.QAbstractTableModel):
    def setData(self, index, value, role):
        # VALIDATE HERE
        if index.column() == 0:
            return False
        if value == 'Some word' or value == '':
            return False
        # END VALIDATE HERE
        if index.isValid() and role == QtCore.Qt.EditRole:
            self.cached[index.row()][index.column()] = QtCore.QVariant(value)
            self.emit(QtCore.SIGNAL("dataChanged(QModelIndex, QModelIndex)"), index, index)
            return True
        else:
            return False
Kelly Keller-Heikkila
  • 2,544
  • 6
  • 23
  • 35
Kamo Petrosyan
  • 214
  • 1
  • 9