4

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.

helsinki
  • 743
  • 7
  • 14
  • Do you constantly change the model? – jdi Dec 27 '12 at 06:20
  • Nope, what makes the big difference is just whether there is data(i.e. item) in it. – helsinki Dec 27 '12 at 06:32
  • Well I was asking because the hidden columns should stick when the model data changes...but probably not when you are changing the model to another one. – jdi Dec 27 '12 at 06:34
  • I do not change the model, there is just one model, and the model cannot fetch all the items during the initialization, but later with a method to load data into it. So the model doesn't change, neither should the view. – helsinki Dec 27 '12 at 06:46
  • Right, but once you set the column hidden after it pulls data the first time, it should stay that way in the view. The problem is that the view is never given a dimension of the model until it gets some data. What type of model are you using? If its a QStandardItemModel, do you init it with a row/col value? – jdi Dec 27 '12 at 06:48
  • So...what i should do is to hide the column after the data is loaded? – helsinki Dec 27 '12 at 06:54
  • Yes, i use QStandardItemModel and i init it with just a column value because the rowCount is uncertain. – helsinki Dec 27 '12 at 07:03

2 Answers2

2

Do this:

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

in the modelReset() slot:

void SomeClass::modelReset()
{
  tableView->hideColumn(0);
}
user1095108
  • 14,119
  • 9
  • 58
  • 116
1

First you set a data model, then tweak its GUI representation:

tableView->setModel(dataModel);
tableView->hideColumn(0);
hank
  • 9,553
  • 3
  • 35
  • 50
  • Yeah..But you said above is exactly what i did.. The point is that at first there's **NO** data in it and then i add data into it, and the hidden column just showed up. – helsinki Dec 27 '12 at 06:26
  • You must be changing a data model, not data in it. Show us your code. – hank Dec 27 '12 at 06:52
  • When you pull data into your model, the model most likey resets. This could mean anything, including the addition/deletion of columns. The GUI resets along with the model. – user1095108 Dec 27 '12 at 07:20