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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章