实现点击下拉列表的项对应的跳转到相应的堆栈窗口,且每个堆栈窗口中都有各自的树形控件,更换可执行文件所在目录下的文件data.json后,点击更新按钮,可以更新所有堆栈窗口的树形控件。
下面是实现代码:
main.cpp
#include "dialog.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include
#include
#include
#include
typedef struct sceneInfo
{
QString name;//场景名
QString id;//场景id
}ST_SCENEINFO;
typedef ST_SCENEINFO stuSceneInfo;
typedef QMap<QString,QList<stuSceneInfo>> mapGroupScene;//键-group,值-组下的所有的场景(场景名和id)
typedef struct groupScene
{
int sid;//组名(可以为空字符串)
mapGroupScene group;//组下的所有场景的名称
}ST_GROUPSCENE;
typedef ST_GROUPSCENE stuGroupScene;
QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
private:
void initInterface();//初始化界面
void initCombox();//初始化下拉列表
void initTreeWidget();//初始化树状列表
void initStackWidgetHw();//初始化华为堆栈窗口
void initStackWidgetTx();//初始化腾讯堆栈窗口
void initStackWidgetWy();//初始化网易堆栈窗口
void initStackWidgetXm();//初始化小木堆栈窗口
void createTreeWidget(mapGroupScene &groupTree,QTreeWidget *widget);//创建树状图
void deleteAllTreeItem();//删除所有页的所有树状项
QByteArray readJsonFile();//读json文件
void parseJson(QByteArray &byteArray);//解析json字符串
private slots:
void onCurrentIndexWidget(int index);//选择下拉列表中的项,对应的窗口也改变
void on_updateBtn_clicked();//更新树状控件
private:
Ui::Dialog *ui;
QMap<int,stuGroupScene> m_sceneInfo;//场景信息,键-sid,值-屏组下的组列表
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include
#include
#include
#include
#include
#include
#include
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
QByteArray byteArray = readJsonFile();
parseJson(byteArray);
initInterface();
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::initInterface()
{
initCombox();
initTreeWidget();
}
void Dialog::initCombox()
{
QStringList strCompany;
strCompany<<QStringLiteral("华为")<<QStringLiteral("腾讯")<<QStringLiteral("网易")
<<QStringLiteral("小米");
ui->comboBox->addItems(strCompany);
void (QComboBox:: *currentIndex)(int index) = &QComboBox::currentIndexChanged;
connect(ui->comboBox,currentIndex,this,&Dialog::onCurrentIndexWidget);
}
void Dialog::initTreeWidget()
{
initStackWidgetHw();
initStackWidgetTx();
initStackWidgetWy();
initStackWidgetXm();
}
void Dialog::initStackWidgetHw()
{
ui->treeWidget->setColumnCount(1);//设置有1列
ui->treeWidget->header()->hide();//设置隐藏头
QMap<int,stuGroupScene>::iterator it = m_sceneInfo.find(1);
if (it != m_sceneInfo.end()) {
stuGroupScene hwScene = it.value();
mapGroupScene hwGroupTree = hwScene.group;
createTreeWidget(hwGroupTree,ui->treeWidget);
}
}
void Dialog::initStackWidgetTx()
{
ui->treeWidget_2->setColumnCount(1);
ui->treeWidget_2->header()->hide();
QMap<int,stuGroupScene>::iterator it = m_sceneInfo.find(4);
if (it != m_sceneInfo.end()) {
stuGroupScene txScene = it.value();
mapGroupScene txGroupTree = txScene.group;
createTreeWidget(txGroupTree,ui->treeWidget_2);
}
}
void Dialog::initStackWidgetWy()
{
ui->treeWidget_3->setColumnCount(1);
ui->treeWidget_3->header()->hide();
QMap<int,stuGroupScene>::iterator it = m_sceneInfo.find(9);
if (it != m_sceneInfo.end()) {
stuGroupScene wyScene = it.value();
mapGroupScene wyGroupTree = wyScene.group;
createTreeWidget(wyGroupTree,ui->treeWidget_3);
}
}
void Dialog::initStackWidgetXm()
{
ui->treeWidget_4->setColumnCount(1);
ui->treeWidget_4->header()->hide();
QMap<int,stuGroupScene>::iterator it = m_sceneInfo.find(12);
if (it != m_sceneInfo.end()) {
stuGroupScene xmScene = it.value();
mapGroupScene xmGroupTree = xmScene.group;
createTreeWidget(xmGroupTree,ui->treeWidget_4);
}
}
void Dialog::createTreeWidget(mapGroupScene &groupTree,QTreeWidget *widget)
{
QMap<QString,QList<stuSceneInfo>>::iterator it;
for (it = groupTree.begin(); it != groupTree.end(); ++it) {
if (!it.key().isEmpty()) {
QTreeWidgetItem *rootItem = new QTreeWidgetItem(widget,QStringList(QString(it.key())));
QList<stuSceneInfo> sceneList = it.value();
for (int i = 0; i < sceneList.size(); ++i) {
stuSceneInfo tempScene = sceneList[i];
QTreeWidgetItem *sceneItem = new QTreeWidgetItem(rootItem,QStringList(QString(tempScene.name)));
}
}else {
QList<stuSceneInfo> sceneList = it.value();
for (int i = 0; i < sceneList.size(); ++i) {
stuSceneInfo tempScene = sceneList[i];
QTreeWidgetItem *sceneItem = new QTreeWidgetItem(QStringList(QString(tempScene.name)));
widget->addTopLevelItem(sceneItem);
}
}
}
}
void Dialog::deleteAllTreeItem()
{
ui->treeWidget->clear();
ui->treeWidget_2->clear();
ui->treeWidget_3->clear();
ui->treeWidget_4->clear();
}
QByteArray Dialog::readJsonFile()
{
QByteArray byteArray;
QString strPath = QCoreApplication::applicationDirPath() + "/data.json";
QFile file(strPath);
if (file.open(QIODevice::ReadOnly)) {
byteArray = file.readAll();
}else {
qDebug()<<QStringLiteral("文件打开失败!");
byteArray = "";
}
return byteArray;
}
void Dialog::parseJson(QByteArray &byteArray)
{
QJsonParseError parseError;
QJsonDocument doc = QJsonDocument::fromJson(byteArray,&parseError);
if (!doc.isNull() && parseError.error == QJsonParseError::NoError) {
QJsonObject rootObject = doc.object();
if (rootObject.value("result").isArray()) {
QJsonArray resultArray = rootObject.value("result").toArray();
for (int i = 0; i < resultArray.size(); ++i) {
QJsonObject sidObject = resultArray[i].toObject();
stuGroupScene tempSidGroup;
mapGroupScene tempGroupMap;
int sid = sidObject.value("sid").toInt();
if (sidObject.value("scene").isArray()) {
QJsonArray sceneArray = sidObject.value("scene").toArray();
for (int j = 0; j < sceneArray.size(); ++j) {
QJsonObject sceneObject = sceneArray[j].toObject();
QString group = sceneObject.value("group").toString();
stuSceneInfo tempScene;
tempScene.id = sceneObject.value("id").toString();
tempScene.name = sceneObject.value("name").toString();
//这段可以简化思路,直接判断map中有没有相应的键,有则改值,无则添加
#if 1
QMap<QString,QList<stuSceneInfo>>::iterator it = tempGroupMap.find(group);
if (it != tempGroupMap.end()) {
QList<stuSceneInfo> &tempSceneList = it.value();
tempSceneList.append(tempScene);
// tempGroupMap.insert(group,tempSceneList);//这步可以不要,虽然要上也没有什么事
}else {
QList<stuSceneInfo> tempSceneList;
tempSceneList.append(tempScene);
tempGroupMap.insert(group,tempSceneList);
}
#endif
#if 0
if (tempGroupMap.size() == 0) {
QList<stuSceneInfo> tempSceneList;
tempSceneList.append(tempScene);
tempGroupMap.insert(group,tempSceneList);
}else {
QMap<QString,QList<stuSceneInfo>>::iterator it = tempGroupMap.find(group);
if (it != tempGroupMap.end()) {
QList<stuSceneInfo> &tempSceneList = it.value();
tempSceneList.append(tempScene);
// tempGroupMap.insert(group,tempSceneList);//这步可以不要,虽然要上也没有什么事
}else {
QList<stuSceneInfo> tempSceneList;
tempSceneList.append(tempScene);
tempGroupMap.insert(group,tempSceneList);
}
}
#endif
}
}
tempSidGroup.sid = sid;
tempSidGroup.group = tempGroupMap;
m_sceneInfo.insert(sid,tempSidGroup);
}
}
}
}
void Dialog::onCurrentIndexWidget(int index)
{
ui->stackedWidget->setCurrentIndex(index);
}
void Dialog::on_updateBtn_clicked()
{
QByteArray byteArray = readJsonFile();
parseJson(byteArray);
deleteAllTreeItem();
initTreeWidget();
}