代码如下:
void MyTableView::doubleClicked(const QModelIndex& index)
{
QModelIndex sourceIndex = proxyModel()->mapToSource(index);
QAbstractItemModel* sourceModel = proxyModel()->sourceModel();
if (sourceModel->rowCount(sourceIndex) > 0) {
sourceIndex = sourceModel->index(sourceIndex.row(), index.column(), sourceIndex.parent());
}
QTableView::doubleClicked(sourceIndex);
}
在双击事件中,首先将当前选中的代理模型的索引映射回源模型的索引,然后判断源模型的当前行数是否大于0,如果是,则表示当前选中的是过滤后的数据,直接使用该索引;否则,表示当前选中的是源模型中的数据,需要重新构建一个源模型的索引。最后调用基类的 doubleClicked 函数处理修改操作。