Search code examples
c++qtmodel-view

QTableView cannot hide column


Well, here is the problem:

  1. I construct a QTableView and use the setModel(myModel) method which bind the model myModel to myTableView;

  2. At first, there's no data in myModel, I use the hideColumn(0) to do the job, and it works well, while after I load data to myModel, the column that I've hidden just shows up.

Would anyone tell me how to hold the hidden state of a column when the model changes?

Any suggestion is appreciated.


OK, here is the code.

void ModelView::createModelAndView()
{

    _TableModel = new TableModel(this);

    _Table = new QTableView(this);

    _Table->setModel(_TableModel);

    _Table->hideColumn(0);      
    _Table->hideColumn(10);     
}

Now the _TableModel has no data.

Then follows this:

_TableModel->loadData();

The loadData() method is used to get data and push data to the model. Right after this step the view(i.e. _Table) changes.


Solution

  • Do this:

    connect(dataModel, SIGNAL(modelReset()), SLOT(modelReset()));
    

    in the modelReset() slot:

    void SomeClass::modelReset()
    {
      tableView->hideColumn(0);
    }