遍历treeWidget节点,索引到要找的节点并选中

void DevMainWindow::indexCurTreeItem(QModelIndex index)
{
    QTableWidgetItem *tableItem = ui->tableWidget->item(index.row(), 5);
    qDebug() << "item.text:::" << tableItem->text();

    itemText1 = ui->tableWidget->item(index.row(), 3)->text();
    itemText2 = ui->tableWidget->item(index.row(), 4)->text();

    int iNum, jNum, kNum;
    QTreeWidgetItem *item1, *item2, *item3;
    //根节点下所有子节点的个数
    iNum = root->childCount();
    //遍历根节点下的所有子节点
    for (int i = 0; i < iNum; i++){
        //根节点下当前遍历到的子节点
        item1 = root->child(i);
        //判断该节点是否是要找的节点
        if (item1->text(0) == itemText1){
            jNum = item1->childCount();
            for (int j = 0; j < jNum; j++){
                item2 = root->child(i)->child(j);
                if (item2->text(0) == itemText2){
                    kNum = item2->childCount();
                    for (int k = 0; k < kNum; k++){
                        item3 = root->child(i)->child(j)->child(k);
                        if (item3->text(0) == tableItem->text()){
                            //找到目标节点并设为选中状态
                            ui->treeWidget->setCurrentItem(item3);
                        }
                    }
                }
            }
        }
    }
}

你可能感兴趣的:(QT)