天天看点

[Qt] QTreeWidget+QStackWidget混合

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