Search code examples
arraysqtqlistqmapqvector

how to store this info in qt? arraylist-ish ?


new bee here. I am sorry if there are similar questions but i just dont even know how to ask it properly. the thing is, i have to do some project for the uni and i am stuck now. i am getting info through udp and need to store the yield info to pass in to objects(car coordinates) i have to do it for 20 objects therefore it is much more complicated. i have this code and it should be working as my prof. told me but i have to figure out how, on my own, to store it. tried QList and QMap but i couldnt figure out how it is possible to have

anArray[num].posX=somenumber;

so i have to store coordinates for each car as car1.x, car1.y, car1z then in the loop count increases; car2.x and so on.

i dont know if my question or what i wanted to ask is clear enough but please bear with me

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;

Solution

  • You can use QList or QVector class for your goal (Qt documentation: QList, QVector).

    Simple example for 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;
        }
    

    Also you can use QMap class (Qt doucmentation: 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;
        }