1

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 ?

4

1 回答 1

1

进入 QStandardItemmodel 实现表明,对于这些函数,除非列存在以开始更新标题数据,否则无效。

因此,可以通过设置模型之前使用的列数来解决此问题

authorModel->setColumnCount(2);

这样,即使模型数据为空列计数也将返回 2,并且在您的情况下调用设置标题数据应该没问题

于 2013-03-09T21:44:36.980 回答