如何将此信息存储在qt中?数组列表?
Posted
技术标签:
【中文标题】如何将此信息存储在qt中?数组列表?【英文标题】:how to store this info in qt? arraylist-ish ? 【发布时间】:2016-06-07 22:40:25 【问题描述】:这里有新蜜蜂。 如果有类似的问题,我很抱歉,但我什至不知道如何正确提问。问题是,我必须为大学做一些项目,我现在被困住了。 我通过 udp 获取信息,需要存储产量信息以传递给对象(汽车坐标) 我必须为 20 个对象做这件事,因此它要复杂得多。 我有这个代码,它应该作为我的教授工作。告诉我,但我必须自己弄清楚如何存储它。 试过 QList 和 QMap 但我不知道怎么可能有
anArray[num].posX=somenumber;
所以我必须将每辆车的坐标存储为 car1.x、car1.y、car1z,然后循环计数增加; car2.x 等等。
我不知道我的问题或我想问的是否足够清楚,但请多多包涵
while(mSocket->hasPendingDatagrams())
QByteArray datagram;
QHostAddress crrAddress;
quint16 crrPort;
datagram.resize( int(mSocket->pendingDatagramSize()) );
mSocket->readDatagram(datagram.data(), datagram.size(), &crrAddress, &crrPort);
double* resultList = new double[(datagram.size() / int(sizeof(double))) ];
memcpy(&resultList[0], datagram.data(), size_t(datagram.size()));
for(quint16 count = 0; (count) < mVehicleCount; count++)
mVehicleMap[mArrayOffset + count].ID = int16_t(resultList[7 + 11 * count]);
mVehicleMap[mArrayOffset + count].pose.position.x = resultList[(1 + 11 * count)];
mVehicleMap[mArrayOffset + count].pose.position.y = resultList[(2 + 11 * count)];
mVehicleMap[mArrayOffset + count].pose.position.z = resultList[(3 + 11 * count)];
mVehicleMap[mArrayOffset + count].pose.orientation.x = resultList[(6 + 11 * count)];
delete[] resultList;
【问题讨论】:
【参考方案1】:您可以使用QList
或QVector
类来实现您的目标(Qt 文档:QList、QVector)。
QVector
的简单示例:
struct Vehicle
qint16 ID_;
double position_x_;
double position_y_;
double position_z_;
double orintation_x_;
;
...
QVector<Vehicle> vehicles(maxVehicleCount); // maxVehicalCount - you variable
...
while(mSocket->hasPendingDatagrams())
QByteArray datagram;
QHostAddress crrAddress;
quint16 crrPort;
datagram.resize( int(mSocket->pendingDatagramSize()) );
mSocket->readDatagram(datagram.data(), datagram.size(), &crrAddress, &crrPort);
double* resultList = new double[(datagram.size() / int(sizeof(double))) ];
memcpy(&resultList[0], datagram.data(), size_t(datagram.size()));
for(quint16 count = 0; (count) < mVehicleCount; count++)
vehicles[mArrayOffset + count].ID_ = qint16(resultList[7 + 11 * count]);
vehicles[mArrayOffset + count].position_x_ = resultList[(1 + 11 * count)];
vehicles[mArrayOffset + count].position_y_ = resultList[(2 + 11 * count)];
vehicles[mArrayOffset + count].position_z_ = resultList[(3 + 11 * count)];
vehicles[mArrayOffset + count].orintation_x_ = resultList[(6 + 11 * count)];
delete[] resultList;
您也可以使用QMap
类(Qt 文档:QMap):
struct Vehicle
qint16 ID_;
double position_x_;
double position_y_;
double position_z_;
double orintation_x_;
;
...
QMap<qint16, Vehicle*> vehicleMap;
...
while(true/*mSocket->hasPendingDatagrams()*/)
QByteArray datagram;
QHostAddress crrAddress;
quint16 crrPort;
datagram.resize( int(mSocket->pendingDatagramSize()) );
mSocket->readDatagram(datagram.data(), datagram.size(), &crrAddress, &crrPort);
double* resultList = new double[(datagram.size() / int(sizeof(double))) ];
memcpy(&resultList[0], datagram.data(), size_t(datagram.size()));
for(quint16 count = 0; (count) < mVehicleCount; count++)
Vehicle* vehicle = new Vehicle();
vehicle.ID_ = qint16(resultList[7 + 11 * count]);
vehicle.position_x_ = resultList[(1 + 11 * count)];
vehicle.position_y_ = resultList[(2 + 11 * count)];
vehicle.position_z_ = resultList[(3 + 11 * count)];
vehicle.orintation_x_ = resultList[(6 + 11 * count)];
vehicleMap.insert(vehicle.ID_, vehicle);
delete[] resultList;
【讨论】:
@AhmetBat 如果对您有帮助,请考虑接受答案 对不起,真的不知道有必要以上是关于如何将此信息存储在qt中?数组列表?的主要内容,如果未能解决你的问题,请参考以下文章