通过点击多层QTreeWidget来控制QStackedWidget中的page页面切换
- treeWidget设计
treeWidget设计:文章来源:https://www.toymoban.com/news/detail-798874.html
// 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级"
完整语句:文章来源地址https://www.toymoban.com/news/detail-798874.html
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);
}
}
到了这里,关于【QT】多层QTreeWidget与QStackedWidget的关联操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!