Qt - Clearing QTableView's contents

thnkwthprtls picture thnkwthprtls · Apr 7, 2014 · Viewed 12.7k times · Source

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!

Answer

Nejat picture Nejat · Apr 7, 2014

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();