Refresh data in view
-
Hello
I am using QAbstractTableModel with QTableView. On one of my views a user is able to delete a row and this works fine. However the view does not update unless you close the window and go back in so looking at the documentation it seems like DataChanged signal would resolve this issue. I have tried it and it does nothing.
Please help.
This is the code for my view
def delete_appointment(self): self.index = self.table.selectionModel().currentIndex() self.s_date = self.index.siblingAtColumn(0).data() self.e_date = self.index.siblingAtColumn(1).data() self.location = self.index.siblingAtColumn(2).data() print(self.s_date, self.e_date, self.location) # for testing self.get_appt_id =main.selected_admin_delete_appt_id(self.s_date,self.e_date,self.location) # call sql query to get appointment id for id in self.get_appt_id: self.get_appt_id1 = id[0] self.table.dataChanged( self.index,self.index,[QtCore.Qt.DisplayRole]) main.admin_delete_appt(self.get_appt_id1)
This is my data method in from my QAbstractTableModel subclass
def data(self, QModelIndex, role= QtCore.Qt.DisplayRole): if role == QtCore.Qt.DisplayRole: # if role is string try: return self.data[QModelIndex.row()][QModelIndex.column()]
-
Hello
I am using QAbstractTableModel with QTableView. On one of my views a user is able to delete a row and this works fine. However the view does not update unless you close the window and go back in so looking at the documentation it seems like DataChanged signal would resolve this issue. I have tried it and it does nothing.
Please help.
This is the code for my view
def delete_appointment(self): self.index = self.table.selectionModel().currentIndex() self.s_date = self.index.siblingAtColumn(0).data() self.e_date = self.index.siblingAtColumn(1).data() self.location = self.index.siblingAtColumn(2).data() print(self.s_date, self.e_date, self.location) # for testing self.get_appt_id =main.selected_admin_delete_appt_id(self.s_date,self.e_date,self.location) # call sql query to get appointment id for id in self.get_appt_id: self.get_appt_id1 = id[0] self.table.dataChanged( self.index,self.index,[QtCore.Qt.DisplayRole]) main.admin_delete_appt(self.get_appt_id1)
This is my data method in from my QAbstractTableModel subclass
def data(self, QModelIndex, role= QtCore.Qt.DisplayRole): if role == QtCore.Qt.DisplayRole: # if role is string try: return self.data[QModelIndex.row()][QModelIndex.column()]
@Nightmaster said in Refresh data in view:
it seems like DataChanged signal would resolve this issue. I have tried it and it does nothing
dataChanged()
is for altering data in existing rows, only. For deleting a row to be noticed byQTableView
and update correctly you must implementremoveRows()
to callbegin
/endRemoveRows()
, and similarly for inserting rows, in yourQAbstractTableModel
-derived class as per https://doc.qt.io/qt-6/qabstracttablemodel.html#subclassing (oh, you are Python, so https://doc.qt.io/qtforpython-6.5/PySide6/QtCore/QAbstractTableModel.html#subclassing). -
See the Editable Tree Model Example .
-
See the Editable Tree Model Example .
-
@Nightmaster said in Refresh data in view:
it seems like DataChanged signal would resolve this issue. I have tried it and it does nothing
dataChanged()
is for altering data in existing rows, only. For deleting a row to be noticed byQTableView
and update correctly you must implementremoveRows()
to callbegin
/endRemoveRows()
, and similarly for inserting rows, in yourQAbstractTableModel
-derived class as per https://doc.qt.io/qt-6/qabstracttablemodel.html#subclassing (oh, you are Python, so https://doc.qt.io/qtforpython-6.5/PySide6/QtCore/QAbstractTableModel.html#subclassing).@JonB Hi Jon, thank you for your help with this.
I have implemented remove rows as the documentation suggests.
def removeRows(self, p_int, p_int_1, parent=None, *args, **kwargs): QAbstractItemModel.beginRemoveRows(QModelIndex(),p_int,p_int_1) self.data.removeAt(p_int) QAbstractItemModel.endRemoveRows()
I am calling it in my delete appointment method (refer to my og post for that code)
TableModel.removeRow(self.appts_data_model, 1, QModelIndex())
This is an excert from another method where i set the datamodel
def admin_view_appts(self): self.view_appts_table = QtWidgets.QWidget() self.view_appts_table.resize(825, 800) # set window size self.get_all_appts = main.admin_view_appts() self.list = [] # initialize list self.view_appt_list = [] **self.appts_data_model = TableModel(self.view_appt_list)** set the model here
I am getting the following error,
" TypeError: beginRemoveRows(self, QModelIndex, int, int): first argument of unbound method must have type 'QAbstractItemModel'
i do not understand because the first arguement of TableModel.removeRow is self.apps_data_model which is of type TableModel which subclasses QAbstractTableModel which inherits from QAbstractItemModel
TableModel.removeRow(self.appts_data_model, 1, QModelIndex())
I hope this makes sense
-
@JonB Hi Jon, thank you for your help with this.
I have implemented remove rows as the documentation suggests.
def removeRows(self, p_int, p_int_1, parent=None, *args, **kwargs): QAbstractItemModel.beginRemoveRows(QModelIndex(),p_int,p_int_1) self.data.removeAt(p_int) QAbstractItemModel.endRemoveRows()
I am calling it in my delete appointment method (refer to my og post for that code)
TableModel.removeRow(self.appts_data_model, 1, QModelIndex())
This is an excert from another method where i set the datamodel
def admin_view_appts(self): self.view_appts_table = QtWidgets.QWidget() self.view_appts_table.resize(825, 800) # set window size self.get_all_appts = main.admin_view_appts() self.list = [] # initialize list self.view_appt_list = [] **self.appts_data_model = TableModel(self.view_appt_list)** set the model here
I am getting the following error,
" TypeError: beginRemoveRows(self, QModelIndex, int, int): first argument of unbound method must have type 'QAbstractItemModel'
i do not understand because the first arguement of TableModel.removeRow is self.apps_data_model which is of type TableModel which subclasses QAbstractTableModel which inherits from QAbstractItemModel
TableModel.removeRow(self.appts_data_model, 1, QModelIndex())
I hope this makes sense
@Nightmaster Just call
self.beginRemoveRows(QModelIndex(),p_int,p_int_1)
-
@JonB Hi Jon, thank you for your help with this.
I have implemented remove rows as the documentation suggests.
def removeRows(self, p_int, p_int_1, parent=None, *args, **kwargs): QAbstractItemModel.beginRemoveRows(QModelIndex(),p_int,p_int_1) self.data.removeAt(p_int) QAbstractItemModel.endRemoveRows()
I am calling it in my delete appointment method (refer to my og post for that code)
TableModel.removeRow(self.appts_data_model, 1, QModelIndex())
This is an excert from another method where i set the datamodel
def admin_view_appts(self): self.view_appts_table = QtWidgets.QWidget() self.view_appts_table.resize(825, 800) # set window size self.get_all_appts = main.admin_view_appts() self.list = [] # initialize list self.view_appt_list = [] **self.appts_data_model = TableModel(self.view_appt_list)** set the model here
I am getting the following error,
" TypeError: beginRemoveRows(self, QModelIndex, int, int): first argument of unbound method must have type 'QAbstractItemModel'
i do not understand because the first arguement of TableModel.removeRow is self.apps_data_model which is of type TableModel which subclasses QAbstractTableModel which inherits from QAbstractItemModel
TableModel.removeRow(self.appts_data_model, 1, QModelIndex())
I hope this makes sense
@Nightmaster said in Refresh data in view:
i do not understand because the first arguement of TableModel.removeRow is self.apps_data_model which is of type TableModel which subclasses QAbstractTableModel which inherits from QAbstractItemModel
Because your
self
= "this" class, where you want to init your remove procedure is yourTableModel
and notQAbstractTableModel
. Calling the super/base class implementationQAbstractItemModel.beginRemoveRows
does not suit your model, since, as the error tells, it expects a genericQAbstractItemModel
and not your custom implementation.
Therefore see @jsulm 's answer. -
@JonB Hi Jon, thank you for your help with this.
I have implemented remove rows as the documentation suggests.
def removeRows(self, p_int, p_int_1, parent=None, *args, **kwargs): QAbstractItemModel.beginRemoveRows(QModelIndex(),p_int,p_int_1) self.data.removeAt(p_int) QAbstractItemModel.endRemoveRows()
I am calling it in my delete appointment method (refer to my og post for that code)
TableModel.removeRow(self.appts_data_model, 1, QModelIndex())
This is an excert from another method where i set the datamodel
def admin_view_appts(self): self.view_appts_table = QtWidgets.QWidget() self.view_appts_table.resize(825, 800) # set window size self.get_all_appts = main.admin_view_appts() self.list = [] # initialize list self.view_appt_list = [] **self.appts_data_model = TableModel(self.view_appt_list)** set the model here
I am getting the following error,
" TypeError: beginRemoveRows(self, QModelIndex, int, int): first argument of unbound method must have type 'QAbstractItemModel'
i do not understand because the first arguement of TableModel.removeRow is self.apps_data_model which is of type TableModel which subclasses QAbstractTableModel which inherits from QAbstractItemModel
TableModel.removeRow(self.appts_data_model, 1, QModelIndex())
I hope this makes sense
@Nightmaster
As the other two have said so far.I am confused by what your
TableModel
class actually is. Is it a subclass ofQAbstractTableModel
? It should be. But have you defined it as some other class which encapsulates (i.e. has a member variable rather than being derived from) which is aQAbstractTableModel
? You need to be subclassingQAbstractTableModel
and overriding the virtual methods in that derived class directly. -
Thanks for your help guys i have solved the issue :).
-