通过点击多层QTreeWidget来控制QStackedWidget中的page页面切换
- treeWidget设计
treeWidget设计:
// treeWidget设计ui->treeWidget->clear();ui->treeWidget->setColumnCount(1);//第一层QStringList l;l<<"管理系统";QTreeWidgetItem *pf = new QTreeWidgetItem(ui->treeWidget,l);ui->treeWidget->addTopLevelItem(pf);//第二层l.clear();l<<"学生管理";QTreeWidgetItem *p1 = new QTreeWidgetItem(pf,l);l.clear();l<<"管理员管理";QTreeWidgetItem *p2= new QTreeWidgetItem(pf,l);pf->addChild(p1);pf->addChild(p2);//第三层l.clear();l<<"学校学生信息管理";QTreeWidgetItem *pi = new QTreeWidgetItem(p1,l);l.clear();l<<"学校学生成绩管理";QTreeWidgetItem *ps = new QTreeWidgetItem(p1,l);p1->addChild(pi);p1->addChild(ps);//第四层l.clear();l<<"2020级";QTreeWidgetItem *p20 = new QTreeWidgetItem(ps,l);l.clear();l<<"2021级";QTreeWidgetItem *p21 = new QTreeWidgetItem(ps,l);l.clear();l<<"2022级";QTreeWidgetItem *p22 = new QTreeWidgetItem(ps,l);l.clear();l<<"2023级";QTreeWidgetItem *p23 = new QTreeWidgetItem(ps,l);//父 子ps->addChild(p20);ps->addChild(p21);ps->addChild(p22);ps->addChild(p23);//第五层l.clear();l<<"自动化专业";QTreeWidgetItem *p201 = new QTreeWidgetItem(p20,l);l.clear();l<<"智能科学与技术专业";QTreeWidgetItem *p202 = new QTreeWidgetItem(p20,l);//父 子p20->addChild(p201);p20->addChild(p202);l.clear();l<<"自动化专业";QTreeWidgetItem *p211 = new QTreeWidgetItem(p21,l);l.clear();l<<"智能科学与技术专业";QTreeWidgetItem *p212 = new QTreeWidgetItem(p21,l);//父 子p21->addChild(p211);p21->addChild(p212);l.clear();l<<"自动化专业";QTreeWidgetItem *p221 = new QTreeWidgetItem(p22,l);l.clear();l<<"智能科学与技术专业";QTreeWidgetItem *p222 = new QTreeWidgetItem(p22,l);//父 子p22->addChild(p221);p22->addChild(p222);l.clear();l<<"自动化专业";QTreeWidgetItem *p231 = new QTreeWidgetItem(p23,l);l.clear();l<<"智能科学与技术专业";QTreeWidgetItem *p232 = new QTreeWidgetItem(p23,l);//父 子p23->addChild(p231);p23->addChild(p232);//第六层//20-自动化l.clear();l<<"1班";QTreeWidgetItem *p2011 = new QTreeWidgetItem(p201,l);l.clear();l<<"2班";QTreeWidgetItem *p2012 = new QTreeWidgetItem(p201,l);l.clear();l<<"3班";QTreeWidgetItem *p2013 = new QTreeWidgetItem(p201,l);//父 子p201->addChild(p2011);p201->addChild(p2012);p201->addChild(p2013);//20-智科l.clear();l<<"1班";QTreeWidgetItem *p2021 = new QTreeWidgetItem(p202,l);l.clear();l<<"2班";QTreeWidgetItem *p2022 = new QTreeWidgetItem(p202,l);//父 子p202->addChild(p2021);p202->addChild(p2022);//21-自动化l.clear();l<<"1班";QTreeWidgetItem *p2111 = new QTreeWidgetItem(p211,l);l.clear();l<<"2班";QTreeWidgetItem *p2112 = new QTreeWidgetItem(p211,l);l.clear();l<<"3班";QTreeWidgetItem *p2113 = new QTreeWidgetItem(p211,l);//父 子p211->addChild(p2111);p211->addChild(p2112);p211->addChild(p2113);//21-智科l.clear();l<<"1班";QTreeWidgetItem *p2121 = new QTreeWidgetItem(p212,l);l.clear();l<<"2班";QTreeWidgetItem *p2122 = new QTreeWidgetItem(p212,l);//父 子p212->addChild(p2121);p212->addChild(p2122);//22-自动化l.clear();l<<"1班";QTreeWidgetItem *p2211 = new QTreeWidgetItem(p221,l);l.clear();l<<"2班";QTreeWidgetItem *p2212 = new QTreeWidgetItem(p221,l);l.clear();l<<"3班";QTreeWidgetItem *p2213 = new QTreeWidgetItem(p221,l);//父 子p221->addChild(p2211);p221->addChild(p2212);p221->addChild(p2213);//20-智科l.clear();l<<"1班";QTreeWidgetItem *p2221 = new QTreeWidgetItem(p222,l);l.clear();l<<"2班";QTreeWidgetItem *p2222 = new QTreeWidgetItem(p222,l);//父 子p222->addChild(p2221);p222->addChild(p2222);//23-自动化l.clear();l<<"1班";QTreeWidgetItem *p2311 = new QTreeWidgetItem(p231,l);l.clear();l<<"2班";QTreeWidgetItem *p2312 = new QTreeWidgetItem(p231,l);l.clear();l<<"3班";QTreeWidgetItem *p2313 = new QTreeWidgetItem(p231,l);//父 子p231->addChild(p2311);p231->addChild(p2312);p231->addChild(p2313);//20-智科l.clear();l<<"1班";QTreeWidgetItem *p2321 = new QTreeWidgetItem(p232,l);l.clear();l<<"2班";QTreeWidgetItem *p2322 = new QTreeWidgetItem(p232,l);//父 子p232->addChild(p2321);p232->addChild(p2322);ui->treeWidget->expandAll();
- 关联
选择on_treeWidget_itemClicked()
函数
重点是使用setCurrentWidget()
和 判断语句
item->text(column) == "3班"
&& item->parent() && item->parent()->text(column) == "自动化专业"
&& item->parent()->parent() && item->parent()->parent()->text(column) == "2020级"
完整语句:
void MainWindow::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
{// 获取当前点击的项的文本// QString clickedItemText = item->text(column);// 根据点击的项进行处理if(item->text(column)=="学校学生信息管理"){ui->stackedWidget->setCurrentWidget(ui->pg_StuInfo);}else if(item->text(column)=="学校学生成绩管理"){ui->stackedWidget->setCurrentWidget(ui->pg_score);}else if (item->text(column) == "1班" && item->parent() && item->parent()->text(column) == "自动化专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2020级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "2班" && item->parent() && item->parent()->text(column) == "自动化专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2020级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "3班" && item->parent() && item->parent()->text(column) == "自动化专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2020级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "1班" && item->parent() && item->parent()->text(column) == "智能科学与技术专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2020级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "2班" && item->parent() && item->parent()->text(column) == "智能科学与技术专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2020级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "1班" && item->parent() && item->parent()->text(column) == "自动化专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2021级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "2班" && item->parent() && item->parent()->text(column) == "自动化专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2021级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "3班" && item->parent() && item->parent()->text(column) == "自动化专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2021级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "1班" && item->parent() && item->parent()->text(column) == "智能科学与技术专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2021级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "2班" && item->parent() && item->parent()->text(column) == "智能科学与技术专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2021级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "1班" && item->parent() && item->parent()->text(column) == "自动化专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2022级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "2班" && item->parent() && item->parent()->text(column) == "自动化专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2022级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "3班" && item->parent() && item->parent()->text(column) == "自动化专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2022级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "1班" && item->parent() && item->parent()->text(column) == "智能科学与技术专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2022级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "2班" && item->parent() && item->parent()->text(column) == "智能科学与技术专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2022级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "1班" && item->parent() && item->parent()->text(column) == "自动化专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2023级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "2班" && item->parent() && item->parent()->text(column) == "自动化专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2023级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "3班" && item->parent() && item->parent()->text(column) == "自动化专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2023级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "1班" && item->parent() && item->parent()->text(column) == "智能科学与技术专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2023级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}else if (item->text(column) == "2班" && item->parent() && item->parent()->text(column) == "智能科学与技术专业" && item->parent()->parent() &&item->parent()->parent()->text(column) == "2023级"){ui->stackedWidget->setCurrentWidget(ui->pg_20auto1);}}