8

I am working in Qt 4.7, and have a dialog containing (among other, unrelated things) a QTableView and a QButton. When the QButton is clicked, it must clear all the data from the QTableView. I am unsure on how to accompish this. I've looked around online for a while, but haven't found anything too helpful. Based on what I found here, I tried this:

void MyClass::on_myButton_clicked() { myTableView->model()->clear(); }

However, this gave the following error:

error: C2039: 'clear' : is not a member of 'QAbstractItemModel'

Is there another way to do this that I am accidentally overlooking? Thanks!

braX
  • 11,506
  • 5
  • 20
  • 33
thnkwthprtls
  • 3,287
  • 11
  • 42
  • 63

2 Answers2

10

I would reset the model (if you do not need the data in the model later). Subclass your model (if it is a custom one) and implement a slot like;

void clear(){
   this->beginResetModel();
   ... // clear the content of your model here
   this->endResetModel();
{

Just check QAbstractItemView::reset().

JBES
  • 1,512
  • 11
  • 18
OnWhenReady
  • 939
  • 6
  • 15
10

The function myTableView->model() returns a QAbstractItemModel which does not contain the clear() method. You should call clear method of your model. If you have a model like:

QStandardItemModel * model= new QStandardItemModel( 2, 4 );

Calling clear should delete all data from the model erasing the view as a consequence as it is provided to show data in the associated model:

model->clear();
Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Nejat
  • 31,784
  • 12
  • 106
  • 138