关于QJsondocument和QJsonObject的问题。

chaney_f 2015-05-18 01:42:19
遇到一个比较奇怪的问题,找不到原因,请教下~
贴一小段代码比较清楚:
QJsonObject mainObj = myProJsonDoc.object();
QJsonObject modulesObj = mainObj.take(QLatin1String("modules")).toObject();
modulesObj.remove(QString::number(tempItem->moduleID));
qDebug()<<modulesObj;
qDebug()<<mainObj;
mainObj.insert(QLatin1String("modules"),modulesObj);
qDebug()<<mainObj<<(QJsonValue)modulesObj;

这种情况下有些时候程序会报segmentation false错,有些时候会有输出,但是输出是错的:
QJsonObject({"0":{"info":"balabala..","input":{"0":""},"name":"test1","output":{"0":""}}})
QJsonObject({})
QJsonObject({"modules":{"ƀ":null}}) QJsonValue(object, QJsonObject({"0":{"info":"balabala..","input":{"0":""},"name":"test1","output":{"0":""}}}) )
但是当把modulesObj.remove(QString::number(tempItem->moduleID));这句话除去的话,就所有输出都是正常的:
QJsonObject mainObj = myProJsonDoc.object();
QJsonObject modulesObj = mainObj.take(QLatin1String("modules")).toObject();
// modulesObj.remove(QString::number(tempItem->moduleID));
qDebug()<<modulesObj;
qDebug()<<mainObj;
mainObj.insert(QLatin1String("modules"),modulesObj);
qDebug()<<mainObj<<(QJsonValue)modulesObj;

输出:
QJsonObject({"0":{"info":"balabala..","input":{"0":""},"name":"test1","output":{"0":""}},"1":{"info":"balabala..","input":{"0":""},"name":"","output":{"0":""}}})
QJsonObject({})
QJsonObject({"modules":{"0":{"info":"balabala..","input":{"0":""},"name":"test1","output":{"0":""}},"1":{"info":"balabala..","input":{"0":""},"name":"","output":{"0":""}}}}) QJsonValue(object, QJsonObject({"0":{"info":"balabala..","input":{"0":""},"name":"test1","output":{"0":""}},"1":{"info":"balabala..","input":{"0":""},"name":"","output":{"0":""}}}) )
所以疑惑,为什么执行remove函数之后会有问题,这里实在想不通,请教一下。
...全文
868 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
本片文章的代码#include "chatwindow.h" #include "ui_chatwindow.h" #include #include #include #include #include #include #include ChatWindow::ChatWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::ChatWindow) { ui->setupUi(this); destIP = "255.255.255.0"; destport=8888; udpSocket = new QUdpSocket(this); bool ok = udpSocket->bind(8888); if(!ok){ qDebug() << "Fail to bind port:8888 , error:" json包借读根据不同的message_type值来选择相应的类容 QJsonParseError parseError; qDebug()<JsonDocument jsondoc = QJsonDocument::fromJson(data,&parseError;); if(jsondoc.isNull() || parseError.error != QJsonParseError::NoError){ qDebug() << "Fail to parse json data : " << parseError.errorString(); return; } QJsonObject jsonObject = jsondoc.object(); int message_type = jsonObject["message_type"].toInt(); QString name = jsonObject["name"].toString(); QString content = jsonObject["content"].toString(); short port = (short)jsonObject["port"].toInt(); switch(message_type){ case ChatWindow::LOGIN_MESSAGE: //添加登陆的用户到好友列表 //给登陆的用户发送一个应答消息,告诉他自己在线 doUserLogin(QHostAddress(sender.toIPv4Address()),name,port); break; case ChatWindow::CHAT_MESSAGE: //显示聊天的消息 break; case ChatWindow::ACK_MESSAGE: //添加应答用户到好友列表 doUserAck(QHostAddress(sender.toIPv4Address()),name,port); return; case ChatWindow::QUIT_MESSAGE: //将退出的用户从好友列表中删除 doUserQuit(QHostAddres

16,240

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧