[Qt] QTreeWidget+QStackWidget混合

本文详细介绍了在Qt中实现树形视图(treeWidget)与堆栈窗口(stackWidget)联动的方法,通过解析子项激活事件,根据当前选中节点的位置,调整堆栈窗口显示的内容。特别关注了如何计算当前节点在堆栈中的正确位置,以确保界面的正确展示。

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个子项。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值