1.双击触发子项激活
如果要获取当前所在index,假设只有2级。
int pa_Row = index.parent().row();//父所在的索引
int curIndex = index.row();//当前所在
void ControlWidget::on_treeWidget_activated(const QModelIndex &index)
{
int stackIndex = 0;
int curIndex = index.row();
int pa_Row = index.parent().row();
int topLevel = ui->treeWidget->topLevelItemCount();
for(int i =1 ;i<=topLevel;i++){
if(i-1 < pa_Row)
stackIndex += ui->treeWidget->topLevelItem(i-1)->childCount();
}
if(0 == pa_Row){
buttonBox->setVisible(true);
_stack->setCurrentIndex(curIndex);
}else if(-1 == pa_Row){
}else {
buttonBox->setVisible(false);
_stack->setCurrentIndex(curIndex+stackIndex);
}
}
注意,不要用设计师里面的stackwidget,不然原始栈会多2个子项。
本文详细介绍了在Qt中实现树形视图(treeWidget)与堆栈窗口(stackWidget)联动的方法,通过解析子项激活事件,根据当前选中节点的位置,调整堆栈窗口显示的内容。特别关注了如何计算当前节点在堆栈中的正确位置,以确保界面的正确展示。
831

被折叠的 条评论
为什么被折叠?



