【Qt】使用Json文件生成QTreeView的树结构(二)

  • 这段代码的主要思路是读取 JSON 文件,构建树形结构,并使用 QFileSystemWatcher 监视文件变化。当文件发生变化时,重新读取 JSON 文件并更新树形结构。这使得用户可以在外部修改 JSON 文件并实时看到更新的树形结构。
#include <QApplication>
#include <QTreeView>
#include <QStandardItemModel>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonValue>
#include <QFile>
#include <QFileSystemWatcher>
#include <QDebug>

// 向 QStandardItemModel 中添加 JSON 数据并构建树形结构的递归函数
void addJsonToTree(QStandardItem* parentItem, const QJsonObject& jsonObject, const QJsonObject& jsonObjectall) {
    // 获取布局类型和组件
    QString layoutType = jsonObject["layout"].toString();
    QJsonArray components = jsonObject["components"].toArray();

    // 设置父节点的文本
    parentItem->setText(layoutType);

    // 遍历组件并添加到树中
    for (const QJsonValue& componentValue : components) {
        if (componentValue.isString()) {
            QString componentName = componentValue.toString();
            QStandardItem* item = nullptr;
            if (jsonObjectall.contains(componentName) && jsonObjectall[componentName].isObject()) {
                // 如果组件是一个对象,递归添加子节点
                item = new QStandardItem();
                addJsonToTree(item, jsonObjectall[componentName].toObject(), jsonObjectall);
            } else {
                // 如果组件是一个字符串,直接添加为叶子节点
                item = new QStandardItem(componentName);
            }
            parentItem->appendRow(item);
        }
    }
}

// 更新 QTreeView 中的树形结构
void updateTreeView(QStandardItemModel* model, const QString& filePath) {
    // 读取 JSON 文件内容
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << "无法打开文件: " << file.errorString();
        return;
    }

    QByteArray jsonData = file.readAll();
    file.close();

    // 解析 JSON 数据
    QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonData);
    if (jsonDocument.isNull()) {
        qDebug() << "无法解析JSON数据";
        return;
    }

    QJsonObject jsonObject = jsonDocument.object();

    // 清空模型,准备添加新的树形结构
    model->clear();

    // 创建根节点并递归添加子节点
    QStandardItem* rootItem = new QStandardItem();
    addJsonToTree(rootItem, jsonObject["whole"].toObject(), jsonObject);
    model->appendRow(rootItem);
}

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // 指定 JSON 文件路径
    QString filePath = "./Tree.json";

    // 创建 QTreeView 和 QStandardItemModel
    QTreeView treeView;
    treeView.setHeaderHidden(true);
    QStandardItemModel model;
    treeView.setModel(&model);

    // 初始化时更新树形结构
    updateTreeView(&model, filePath);

    // 创建 QFileSystemWatcher 来监视文件变化
    QFileSystemWatcher watcher;
    watcher.addPath(filePath);

    // 当文件发生变化时,更新树形结构
    QObject::connect(&watcher, &QFileSystemWatcher::fileChanged, [&model, &filePath]() {
        updateTreeView(&model, filePath);
    });

    // 显示窗口
    treeView.show();

    return app.exec();
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值