与此问题相关的源代码可在我的 BitBucket 上的公共 Git 存储库中找到。
我正在尝试使用mainwindow.cppQTreeView中的以下代码将一些项目动态添加到模型中:
if(dlg->exec() == QDialog::Accepted) {
QList<QVariant> qList;
qList << item.name << "1111 0000" << "0x00";
HidDescriptorTreeItem *item1 = new HidDescriptorTreeItem(qList, hidDescriptorTreeModel->root());
hidDescriptorTreeModel->root()->appendChild(item1);
}
MainWindow这在我的构造函数中运行时有效,就在 之后ui->setupUi(this),但我需要它从事件过滤器中运行,但相同的代码没有得到QTreeView更新。当我设置断点mainwindow.cpp:70并逐步执行接下来的几行时,我可以看到数据正在添加到模型中,但我需要QTreeView刷新。
我知道这是通过 emitting 完成的dataChanged(),但不确定如何执行此操作。信号的信号签名dataChanged如下所示:
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>());
topLeft所以我需要想出bottomRight QModelIndex实例。如何从item1上面的代码片段中构建/获取这些?
另外,beginInsertRows()我endInsertRows()应该在哪里调用这些函数?