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?
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.