天天看點

[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個子項。