实现代码
model 为私有变量,QStandardItemModel *model;
void ParamSettingDlg::GetNode()
{
for(int i = 0;i < model->rowCount() ;i++)
{
QStandardItem *item = model->item(i);
qDebug() <<"item = " << item->text();
GetItem(model->item(i) );
}
}
GetItem 为递归函数,这样可以遍历到所有的节点
void ParamSettingDlg::GetItem(QStandardItem *item)
{
Q_ASSERT(item);
if(item->hasChildren())
{
for(int i = 0;i < item->rowCount() ;i++)
{
QStandardItem * childitem = item->child(i);
qDebug() << "childitem = " << childitem->text();
GetItem(childitem);
}
}
}
遍历所有节点查找并选中这一节点
for(int i = 0;i < standardModel_down->rowCount() ;i++)
{
QStandardItem *item = standardModel_down->item(i);
Q_ASSERT(item);
if(item->hasChildren())
{
for(int i = 0;i < item->rowCount() ;i++)
{
QStandardItem * childitem = item->child(i);
qDebug() << "childitem = " << childitem->text();
if(childitem->text() == fileName1)
{
ui->treeView_down->setCurrentIndex(childitem->index());
}
}
}
qDebug() <<"item = " << item->text();
}