Reputation: 13105
I have a piece of code in my application as follows :
....
QStandardItemModel* authorModel = getAuthorModel(author);
// Create result tab
QTableView* tblView = new QTableView();
tblView->setModel(authorModel);
controller.queryAuthor(author, authorModel);
qDebug() << authorModel->setHeaderData(0, Qt::Horizontal, QVariant("Author Name"), Qt::DisplayRole);
qDebug() << authorModel->setHeaderData(1, Qt::Horizontal, QVariant("Author Id"), Qt::DisplayRole);
int tabIdx = ui->mainTabWidget->addTab(tblView, author);
ui->mainTabWidget->setCurrentIndex(tabIdx);
tblView->setColumnHidden(1, true);
This code is called multiple times creating different tableviews. When authorModel is empty, then setting headerdata fails, also, setColumnHidden fails and once the data is populated default numeric headers are shown and column 1 is visible. Both qDebug statements return false.
However, when the same populated model is used to create a new table view, in the new view column 1 is hidden without issues, and headers are set as they should be. Both qDebug statements return true.
What is the problem and how can it be alleviated ?
Upvotes: 1
Views: 132
Reputation: 17380
Stepping into QStandardItemmodel implementation shows that for these functions unless the column exists to begin with updating header data has no effect.
This can thus be worked around by setting the number of columns your model is designed to use before by using
authorModel->setColumnCount(2);
This way even if the model data is empty column count will return 2 and the calls to set header data should be fine in your case
Upvotes: 1