PyQt reset tableView
-
Dear All,
I am new to pyqt and using tableView in my application.
I am inserting rows to tableview. And I wish to clear the tableView(all row entries in table) once I push pushbutton.
Searched online, but could not find exact solution.Could anyone help me in knowing how to clear rows in tableView or how to reset
tableview in pyqt.Thanks
-
Hi
What kind of model are you using with your qtableview ? -
Hi
So when you say "clear the view"
do you want to delete the data from the model or simply
remove the whole model from the view?The view only displays the data. and while it can filter it or sort it,
the model is the one that can clear the data.
If by clear you mean delete it.
Normally that would happen by removing the data from the original data structure
that is feeding the model. -
Hi
So when you say "clear the view"
do you want to delete the data from the model or simply
remove the whole model from the view?The view only displays the data. and while it can filter it or sort it,
the model is the one that can clear the data.
If by clear you mean delete it.
Normally that would happen by removing the data from the original data structure
that is feeding the model. -
Hi
Then just call setModel on view with null pointer.
It will then disconnect the model and show nothing. -
@mrjj
actually I am trying to display certain set of rows in table based on key entered and when I enter new key in textedit and push button it should display the rows related to entered key@sushmita
Hehe so that is very different from clearing the view.You might want to use a proxy model to do the filtering.
https://doc.qt.io/qtforpython/PySide2/QtCore/QSortFilterProxyModel.htmlexample
https://stackoverflow.com/questions/14068823/how-to-create-filters-for-qtableview-in-pyqt -
Hi,
@mrjj ,Thanks for your suggestion, I tried using proxy model.
With this I can filter fields in same table view.But, I am trying to display the filtered data in different table view .
i.e one main table view will be showing all the entries, and once I enter the key, the filtered data is displayed in other table view.So while doing the same I am able to filter data and display in separate table view, but when I am entering new key and pushing button, the table view is displaying the new filtered data along with previous list.
I.e my need is to refresh the table view every time I enter key and push the button and display new filtered data.
Thanks,
Sushmita -
Hi,
Can you show the code related to the model and filtering ?
-
-
Why a custom table model ? Since you are using a QSortFilterProxyModel, just assign that one to the view showing the filtered data. There's no need for that second model.
-
Why a custom table model ? Since you are using a QSortFilterProxyModel, just assign that one to the view showing the filtered data. There's no need for that second model.
-
Sorry, I misunderstood your answer to @mrjj.
In any case, that would likely be the solution. You have a view on your complete data, then on the filtered view, you put the QSortFilterProxyModel as model on top of your main data. That way you don't have to manually add/remove stuff from a secondary model that would duplicate the data already available.
-
Sorry, I misunderstood your answer to @mrjj.
In any case, that would likely be the solution. You have a view on your complete data, then on the filtered view, you put the QSortFilterProxyModel as model on top of your main data. That way you don't have to manually add/remove stuff from a secondary model that would duplicate the data already available.
-
Ok. Thanks .
I thought of trying without using QSortFilterProxyModel
I will try .Hi,
Can we use QSortFilterProxyModel with QAbstractTableModel?
Cause the example which was suggested proxy model I referred is using QStandardItemModel.But in my application I am usign QAbstractTableModel for data model.
Thanks,
Sushmita -
No problem with that.