问题遇到的现象和发生背景
Qt Tcpscoket QByteArray 发送自定义结构体: 发送端将结构体转QByteArray 再转结构体结构正确,但在接收端将Qbytearray转结构体程序崩溃
问题相关代码,请勿粘贴截图
struct Info
{
int numMax;
int id;
QVector v_name;
};
struct Data{
QVector v_info;
};
//发送端
Data data;
int num = 5;
for(int i = 2; i < num; i++)
{
Info info;
info.id = i;
info.numMax = i;
for(int j = 0; j < i; j++)
{
info.v_name.push_back("player"+QString::number(j));
}
data.v_info.push_back(info);
}
QByteArray arr;
arr.append(reinterpret_cast<char *>(&data), sizeof(Data));
socket->write(arr);
//接收端
QByteArray arr = socket->readAll();
Data *getMsg = new Data;
getMsg = reinterpret_cast<Data *>(arr.data());
// getMsg2 = (Data *)arr.data();
for(auto &i:getMsg->v_info)
{
qDebug() << i.id <<" " << i.numMax;
for(int j = 0; j<i.v_name.size(); j++)
{
qDebug()<< i.v_name.at(j);
}
}
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
2 2
"player0"
"player1"
3 3
"player0"
"player1"
"player2"
4 4
"player0"
"player1"
"player2"
"player3"