Dynamically set flags of a QAbstractTableModel
-
Hi, I am writing an application where a QAbstractTableModel is being used with a QTableView to display some data.
At some point, the user will modify the data that is being displayed, and I would like to disable the row corresponding to the data in the view.
What I am doing right now is to simply call QAbstractTableModel.dataChanged.emit() when the data is changed somewhere in the program, so that the flags() function (un)sets the flags of the row depending on the data, thus disabling it.
e.g.:
class Whatever(): def disable(): my_data_element.enabled = False; self.table_model.dataChanged.emit(self.image_model.index(my_data_element.row , 0), #topleft self.image_model.index(my_data_element.row, 1), #bottomright {Qt.DisplayRole, Qt.EditRole, Qt.CheckStateRole}) ... class ImageLayerModel(QAbstractTableModel): def flags(self, index: QModelIndex) -> Qt.ItemFlags: #Disabled Row if not self.my_data[index.row()].enabled: return Qt.NoItemFlags ...
This is working as expected but I'm still confused about the correct functionality of the class and want to avoid future problems.
Is this a safe way to do it, or should I be using the setData() funtion with a UserRole to change my_data_element and set the flag automatically?
Or is there any better way at all to do this?Im a newbie so I appreciate any comments.
Thanks in advance. -
Hi and welcome to devnet,
The disabling should happen in the setData method directly since it is supposed to happen when you change some data.
-
Hi and welcome to devnet,
The disabling should happen in the setData method directly since it is supposed to happen when you change some data.
-
No, you still have to implement the flag method since the flag values depend on a special event in your application. What I meant is that you want to disable a row when something was edited in it. The edit happens through setData where you modify your data structure so there you have all the information needed to disable the whole row. No need for an "external" call.