• QT json数据的应用(cJSON)


    json数据可保存小量的数据在本地的json文件中。QT有两种方式操作:(1).cJSON (2).QT的操作json数据的类。

    应用:将监控预案数据保存在本地中。

    1.首先根据预案结构创建一个json格式的数据框架:

    2.将监控预案树转化成json数据,再保存为json文件。

    void VideoPreview::setPlanTreeDataToJsonFile()
    {
        //将m_pPlanTree预案数据转化成json数据写入文件
        cJSON* pData = cJSON_CreateArray();
    
        int count = m_pPlanTree->topLevelItemCount();
        for (int i = 0; i < count; i++)
        {
            cJSON* pObject = cJSON_CreateObject();
            cJSON_AddItemToArray(pData, pObject);
    
            QTreeWidgetItem* pTopItem = m_pPlanTree->topLevelItem(i);
            QString text = pTopItem->text(0);
            cJSON_AddItemToObject(pObject, "name", cJSON_CreateString(text.toStdString().c_str()));
    
            cJSON* pDataArray = cJSON_AddArrayToObject(pObject, "data");
    
            int topChildCount = pTopItem->childCount();
            for (int j = 0; j < topChildCount; j++)
            {
                int childCount = pTopItem->child(j)->childCount();
                for (int k = 0; k < childCount; k++)
                {
                    int childChildCount = pTopItem->child(j)->child(k)->childCount();
                    for (int n = 0; n < childChildCount; n++)
                    {
                        QTreeWidgetItem* pChildChildItem = pTopItem->child(j)->child(k)->child(n);
                        QVariant var = pChildChildItem->data(0, Qt::UserRole);
                        VIDEO_CHANNEL vc = var.value<VIDEO_CHANNEL>();
                        VIDEO_DEVICE chData = vc.device;
    
                        cJSON* pObj = chData.covertToJson();
                        cJSON_AddItemToArray(pDataArray, pObj);
                    }
                }
            }
        }
    
        //写入到文件(覆盖)
        writePreviePlan(pData);
    }
    void VideoPreview::writePreviePlan(cJSON * data)
    {
        QString filePath = QApplication::applicationDirPath() + "/previewPlanConfig.json";
        QFile file(filePath);
        bool bRet = file.open(QIODevice::WriteOnly | QIODevice::Text);
        if (bRet) {
            file.write(cJSON_PrintUnformatted(data));
        }
    }

    3.当监控预案树初始化时:

    void VideoPreview::reloadPreviePlan()   //重新从json文件中加载预案数据到m_pPlanTree
    {
        m_pPlanTree->clear();
        cJSON* pData = readPreviewPlan();
        QString str = cJSON_Print(pData);
        
        if (pData) {
            int size = cJSON_GetArraySize(pData);
    
            for (int i = 0; i < size; i++) {
                cJSON* pArray = cJSON_GetArrayItem(pData, i);
                cJSON* pName = cJSON_GetObjectItem(pArray, "name");   //预案名称
                QString planName = cJSON_Print(pName);
                planName = planName.replace(""", "");
    
    
                //添加顶层节点
                QTreeWidgetItem* pRootPlanItem = new QTreeWidgetItem(m_pPlanTree, QStringList() << planName);
                pRootPlanItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);   //设置可编辑
                pRootPlanItem->setData(0, Qt::UserRole + 1, QVariant(TREE_ITEM_TYPE::TREE_PLAN_ROOT));
                pRootPlanItem->setIcon(0, QIcon(":/Images/Resources/video_plan.png"));
    
                m_pPlanTree->addTopLevelItem(pRootPlanItem);   //添加顶层节点
    
    
                //在根节点下添加数据
                cJSON* p_Data = cJSON_GetObjectItem(pArray, "data");   //数据
                int dataSize = cJSON_GetArraySize(p_Data);
                for (int i = 0; i < dataSize; i++)
                {
                    cJSON* item = cJSON_GetArrayItem(p_Data, i);
    
                    VIDEO_DEVICE chData(item);
    
                    //将视频监控按名称->地点->位置分类
                    if (pRootPlanItem->childCount() == 0) {
                        createChildItem(pRootPlanItem, chData, i, TREE_ITEM_TYPE::TREE_VIDEO_NAME);
                        createChildItem(pRootPlanItem->child(0), chData, i, TREE_ITEM_TYPE::TREE_VIDEO_ADDR);
                        createChildItem(pRootPlanItem->child(0)->child(0), chData, i, TREE_ITEM_TYPE::TREE_PLAN_CHANNEL);
                        continue;
                    }
    
                    for (int j = 0; j < pRootPlanItem->childCount(); j++)   //湖南省
                    {
                        QTreeWidgetItem* nameItem = pRootPlanItem->child(j);
                        if (chData.name == nameItem->text(0)) {
    
                            if (nameItem->childCount() == 0) {
                                createChildItem(nameItem, chData, i, TREE_ITEM_TYPE::TREE_VIDEO_ADDR);
                            }
    
                            for (int k = 0; k < nameItem->childCount(); k++)   //衡阳市
                            {
                                QTreeWidgetItem* addrItem = nameItem->child(k);
                                if (chData.addr == addrItem->text(0)) {
    
                                    for (int n = 0; n < addrItem->childCount(); n++)   //衡东县
                                    {
                                        QTreeWidgetItem* locationItem = addrItem->child(n);
                                        if (chData.location == locationItem->text(0)) {
    
                                            //相同的预案则不重复添加
                                            break;
                                        }
                                        else {
                                            if (n < addrItem->childCount() - 1)
                                                continue;
                                        }
    
                                        //所有级都没有相同的则创建一个并保存返回
                                        createChildItem(addrItem, chData, i, TREE_ITEM_TYPE::TREE_PLAN_CHANNEL);
                                        break;
                                    }
                                    break;
                                }
                                else {
                                    if (k < nameItem->childCount() - 1)
                                        continue;
                                }
                                createChildItem(nameItem, chData, i, TREE_ITEM_TYPE::TREE_VIDEO_ADDR);
                                createChildItem(nameItem->child(k + 1), chData, i, TREE_ITEM_TYPE::TREE_PLAN_CHANNEL);
                                break;
                            }
                            break;
                        }
                        else {
                            if (j < pRootPlanItem->childCount() - 1)
                                continue;
                        }
                        createChildItem(pRootPlanItem, chData, i, TREE_ITEM_TYPE::TREE_VIDEO_NAME);
                        createChildItem(pRootPlanItem->child(j + 1), chData, i, TREE_ITEM_TYPE::TREE_VIDEO_ADDR);
                        createChildItem(pRootPlanItem->child(j + 1)->child(0), chData, i, TREE_ITEM_TYPE::TREE_PLAN_CHANNEL);
                        break;
                    }
                }
            }
        }
    }
    
    void VideoPreview::createChildItem(QTreeWidgetItem *parent, VIDEO_DEVICE data, int channel, TREE_ITEM_TYPE type)
    {
        QString text;
        if (type == TREE_ITEM_TYPE::TREE_VIDEO_NAME) {
            text = data.name;
        }
        else if (type == TREE_ITEM_TYPE::TREE_VIDEO_ADDR) {
            text = data.addr;
        }
        else if (type == TREE_ITEM_TYPE::TREE_VIDEO_CHANNEL || type == TREE_ITEM_TYPE::TREE_PLAN_CHANNEL) {
            text = data.location;
        }
    
        QTreeWidgetItem* chItem = new QTreeWidgetItem(parent, QStringList() << QString(text));
        chItem->setIcon(0, QIcon(":/Images/Resources/video_icon.png"));
        chItem->setToolTip(0, data.name + "-" + data.addr + "-" + data.location);
    
        if (type == TREE_ITEM_TYPE::TREE_VIDEO_CHANNEL || type == TREE_ITEM_TYPE::TREE_PLAN_CHANNEL) {
            VIDEO_CHANNEL chInfo;
            chInfo.device.copyFrom(data);
            chInfo.nChannel = channel;
            QVariant varChInfo;
            varChInfo.setValue(chInfo);
            chItem->setData(0, Qt::UserRole, varChInfo);
        }
    
        QVariant itemType(type);
        chItem->setData(0, Qt::UserRole + 1, itemType);
    }
    cJSON* VideoPreview::readPreviewPlan()
    {
        cJSON* pRet = NULL;
        QString filePath = QApplication::applicationDirPath() + "/previewPlanConfig.json";
        QFile file(filePath);
        bool bRet = file.open(QIODevice::ReadWrite | QIODevice::Text);
        if (bRet) {
        
            QByteArray ar = file.readAll();
            pRet = cJSON_Parse(ar.toStdString().c_str());
            file.close();
        }
        return pRet;
    }
  • 相关阅读:
    java+SpringCloud开发的性能和环保问题
    java基础匿名类/对象
    开发工具eclipse/idea 在运行前执行一些动作
    java基础super关键字
    SpringCloud开发之OpenFeign timeout和压缩等问题
    日志之log4j2和springboot
    dubbogo 连载1
    Builder和Factory设计模式理解
    cm升级
    mysql主备安装及命令
  • 原文地址:https://www.cnblogs.com/tingtaishou/p/12053555.html
Copyright © 2020-2023  润新知