Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top