QTreeView 如何展开/收缩 该节点下所有子节点?

liuhelh 2011-07-07 02:37:11
貌似qt没有自带展开/收缩所有当前节点所有子节点的功能。

坛子里有没有人以前实现过?
...全文
3389 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
many_many 2011-08-08
  • 打赏
  • 举报
回复
使用函数 void collapse ( const QModelIndex & index )
直接展开
liuhelh 2011-07-10
  • 打赏
  • 举报
回复

用递归实现了
下面是 collapse 所有子节点的代码
expand 所有子节点的代码类似,或者在递归函数上加一个 bool 参数就能实现。

/*collapse all child nodes*/
int childCount = model->rowCount(index);
recursive_collapse(index,childCount);

void WellImportDialog::recursive_collapse(const QModelIndex index,int childCount)
{

for(int childNo=0;childNo<childCount;childNo++)
{
QModelIndex childIndex = index.child(childNo,0);
if(ui->treeView_dir->isExpanded(childIndex))
{
ui->treeView_dir->setExpanded(childIndex,false);
if(int rowCount = model->rowCount(childIndex)>0)
recursive_collapse(childIndex,rowCount);
}
}

}
QQ551780977 2011-07-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ice 的回复:]
expandAll是展开所有节点的,expand你需要轮询调用了
[/Quote]
支持下...我准备用到...
开始领悟 2011-07-07
  • 打赏
  • 举报
回复
expandAll是展开所有节点的,expand你需要轮询调用了
cpio 2011-07-07
  • 打赏
  • 举报
回复
如果没有现成的函数可调用,就只能递归了自己遍历了

16,215

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧