infotree_wgt.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "infotree_wgt.h"
  2. #include "ui_infotree_wgt.h"
  3. #include <QVBoxLayout>
  4. #include <QTreeView>
  5. #include <QStandardItemModel>
  6. #include <QJsonDocument>
  7. #include <QJsonObject>
  8. #include <QJsonArray>
  9. #include <QFile>
  10. #include <QStandardItem>
  11. #include <QMessageBox>
  12. #include "basedisplay_wgt.h"
  13. #include <QString>
  14. InfoTree_wgt::InfoTree_wgt(QWidget *parent) :
  15. QWidget(parent),
  16. ui(new Ui::InfoTree_wgt),
  17. treeView(new QTreeView(this)), // 声明 QTreeView 为类成员
  18. model(new QStandardItemModel(this)) // 声明模型为类成员
  19. {
  20. ui->setupUi(this);
  21. // 读取 JSON 文件
  22. QFile file("E:\\SATpro\\die-bonder-ui\\form\\data.json");
  23. if (!file.open(QIODevice::ReadOnly)) {
  24. qWarning() << "Could not open JSON file.";
  25. return; // 如果文件无法打开,直接返回
  26. }
  27. QByteArray jsonData = file.readAll();
  28. file.close();
  29. if(jsonData.isEmpty())
  30. {
  31. //qWarning() << "1111111";
  32. QMessageBox::information(this,QString("提示"),QString("jsonData is empty"),QMessageBox::Ok);
  33. }
  34. QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
  35. if(jsonDoc.isEmpty())
  36. {
  37. //qWarning() << "5555555";
  38. QMessageBox::information(this,QString("提示"),QString("jsonDoc is empty"),QMessageBox::Ok);
  39. }
  40. if (jsonDoc.isNull() || !jsonDoc.isObject()) {
  41. qWarning() << "Invalid JSON data.";
  42. qWarning() << jsonDoc.isNull();
  43. qWarning() << jsonDoc.isObject();
  44. return; // 如果数据无效,直接返回
  45. }
  46. // 将 JSON 数据转换为树结构
  47. QJsonObject jsonObject = jsonDoc.object();
  48. QStandardItem* rootItem = createTreeFromJson(jsonObject);
  49. model->setItem(0, rootItem); // 从指针调用 setItem
  50. // 将模型设置到 QTreeView
  51. treeView->setModel(model); // 从指针调用 setModel
  52. treeView->setHeaderHidden(true); // 隐藏表头
  53. treeView->setWindowTitle("JSON Tree Viewer"); // 这行不太适合这里
  54. treeView->resize(400, 300); // 这行不太适合这里
  55. // 创建垂直布局并将 QTreeView 添加到布局中
  56. QVBoxLayout *wgt_layout = new QVBoxLayout(this);
  57. wgt_layout->addWidget(treeView); // 只添加一次
  58. setLayout(wgt_layout); // 设置布局
  59. connect(treeView,&QTreeView::clicked,this,&InfoTree_wgt::show);
  60. }
  61. InfoTree_wgt::~InfoTree_wgt() {
  62. delete ui;
  63. }
  64. //static QString str[9] = {QString("home"),QString("production"),QString("program"),
  65. // QString("message"),QString("module"),QString("disposition"),
  66. // QString("diagnosis"),QString("system"),QString("help"),
  67. // };
  68. QStandardItem *InfoTree_wgt::createTreeFromJson(const QJsonObject &jsonObject)
  69. {
  70. QStandardItem* item = new QStandardItem(jsonObject["name"].toString());
  71. if (jsonObject.contains("children")) {
  72. QJsonArray childrenArray = jsonObject["children"].toArray();
  73. for (const auto& childValue : childrenArray) {
  74. QJsonObject childObject = childValue.toObject();
  75. item->appendRow(createTreeFromJson(childObject));
  76. }
  77. }
  78. return item;
  79. }
  80. void InfoTree_wgt::show()
  81. {
  82. BaseDisplay_wgt* showWidght = new BaseDisplay_wgt;
  83. showWidght->show();
  84. }