QTreeWidget可以呈现出数据的层次关系,有时需要对数据进行筛选,只显示结果项,其他项隐藏。如下图所示
通过查询QTreeWidget的方法可以看到有个setRowHidden方法可以隐藏行,Qt assistant中说明如下:
void QTreeView::setRowHidden(int row, const QModelIndex &parent, bool hide)
If hide is true the row with the given parent is hidden, otherwise the row is shown.
注意这是隐藏parent节点下面第row项。现在问题转化为获取需隐藏项的parent的QmodelIndex和该项在parent中的row,再次查找方法可以发现有个indexFromItem方法,Qt assistant中说明如下:
[protected] QModelIndex QTreeWidget::indexFromItem(const QTreeWidgetItem *item, int column = 0) const
Returns the QModelIndex associated with the given item in the given column.
这是个protected方法,QTreeWidget对象无法直接调用,我们必须