How to assign two or more values to a QMap Variable in Qt

I am getting confused of how to store the values assigned from 3 different functions and storing them in a single map variable

QMap<QString,QString> TrainMap = nullptr;   
if(......)    
(    
       TrainMap    =    PrevDayTrainMap();
       TrainMap    =    NextDayTrainMap();
       TrainMap    =    CurrentDayTrainMap();
}

The PrevDayTrainMap,NextDayTrainMap & CurrentDayTrainMap returns a set of values with Date and the TrainIdName.I need to store all the values from prevday,currentday and nextday in the TrainMap but it stores only the currentday values to the TrainMap as it is assigned at the last.I am not sure what to do so that it doesn't overwrite.If I should merge what is the way to do it?

1 answer

  • answered 2022-05-07 14:45 mzimmers

    The reason you're only getting the currentday value in your map is that your three assignments are overwriting each other:

    TrainMap    =    PrevDayTrainMap();
    TrainMap    =    NextDayTrainMap(); // undoes the PrevDayTrainMap()
    TrainMap    =    CurrentDayTrainMap(); // undoes the NextDayTrainMap()
    

    You need to insert each returned value into your map:

    TrainMap.insert(PrevDayTrainMap());
    TrainMap.insert(NextDayTrainMap());
    TrainMap.insert(CurrentDayTrainMap());
    

    As others have pointed out, this will only work if your functions return a valid map element.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum