[SOLVED] Passing in QMap
-
Hi
I have a class.
e.g.
@
class Graha
{
public:
Graha();
QMap<QString, QMap<QString, int> > chartMap;};
@I have filled instances of the Graha class above inside a QMap<QString, Graha> grahaMap.
I am passing a QMap<QString, Graha> grahaMap to a method of another class.
e.g.
@ DivChartBaseClass testing;
testing.calcRootD1(grahaMap);
@Inside the calcRootD1 method I access the QMap<QString, QMap<QString, int> > chartMap of the Graha class instance and assign/insert values into it:
e.g.
@
QMapIterator<QString, Graha> i(grahaMap);
while (i.hasNext()) {
i.next();QMap<QString, int> houseNumAndSign; houseNumAndSign.insert(kHouseNum, howManyHousesAway(rootD1Ascendant, j)); houseNumAndSign.insert(kSignNum, j); Graha g = i.value(); g.chartMap.insert(kD1, houseNumAndSign); qDebug() << "***************************"; qDebug() << i.key()<< ":" << g.chartMap.value(kD1).value(kHouseNum); qDebug() << i.key() << ":" << g.chartMap.value(kD1).value(kSignNum); qDebug() << "**************************";
}
@As you can see from above, I even print the values out to the console just to prove that the values have been inserted to each
QMap<QString, Graha> grahaMap in each of the Graha instances...the printing to console above does confirm that the changes have been made, however, it is short-lived or maybe applied to a "copy" of grahaMap instead of the actual grahaMap.....When I iterate through the values outside of the method, back in the class that originally called the calcRootD1 method....I don't get the values I have assigned.
Here is the code:
@
DivChartBaseClass testing;
testing.calcRootD1(grahaMap);QMapIterator<QString, Graha> i(grahaMap); while (i.hasNext()) { i.next(); Graha g = i.value(); qDebug() << i.key() << ":x " << g.chartMap.value(kD1).value(kHouseNum); qDebug() << i.key() << ":x " << g.chartMap.value(kD1).value(kSignNum); }
@
how do I ensure that the changes that calcRootD1 applies to the objects inside grahaMap take effect???
Thank you very much.
-
Hi
I've changed my code to take in a reference but is still not working:
@
void DivChartBaseClass::calcRootD1(QMap<QString, Graha> &grahaMap)
{
QMapIterator<QString, Graha> i(grahaMap);
while (i.hasNext()) {
i.next();
//qDebug() << i.key() << ": " << i.value().name << " " << degreesToSeconds( i.value().longitude )<< endl;
for(int j = 1; j <= 12; j++){
if(degreesToSeconds( i.value().longitude ) >= d1Border[j-1] && degreesToSeconds( i.value().longitude ) < d1Border[j])
{
if(i.key() == kAsc){
rootD1Ascendant = j;
}
d1GrahaInSign.insert(i.key(), j);
d1GrahaInHouse.insert(i.key(), howManyHousesAway(rootD1Ascendant, j));
QMap<QString, int> houseNumAndSign;
//ASSERT that rootD1Ascendant is not 0
houseNumAndSign.insert(kHouseNum, howManyHousesAway(rootD1Ascendant, j));
houseNumAndSign.insert(kSignNum, j);
Graha g = i.value();
g.chartMap.insert(kD1, houseNumAndSign);
qDebug() << "*";
qDebug() << i.key()<< ":" << g.chartMap.value(kD1).value(kHouseNum);
qDebug() << i.key() << ":" << g.chartMap.value(kD1).value(kSignNum);
qDebug() << "";
}
}
}
}
@The above code prints out the changes made to the Graha instances.
However, the code below, which is in another class (the class that called the above function in the first place), doesn't reflect the changes.@
DivChartBaseClass testing;
testing.calcRootD1(grahaMap);QMapIterator<QString, Graha> i(grahaMap); while (i.hasNext()) { i.next(); Graha g = i.value(); qDebug() << i.key() << ":x " << g.longitude << g.chartMap.value(kD1).value(kHouseNum); qDebug() << i.key() << ":x " << g.labelChinese << g.chartMap.value(kD1).value(kSignNum); }
@
What am i doing wrong???
Thank you....
-
If I am not mistaking:
@
Graha g = i.value();
g.chartMap.insert(kD1, houseNumAndSign);@you are modifying a copy of your map value not the original
-
Hi
Thank you very much..Yes...for some odd reason....
After some experimenting...
I found that this line made the difference of making it work the way I wanted it to work...
grahaMap[i.key()] = g;Here is the whole code for the benefit of other ppl reading these posts:
@
void DivChartBaseClass::calcRootD1(QMap<QString, Graha> &grahaMap)
{QMapIterator<QString, Graha> i(grahaMap); while (i.hasNext()) { i.next(); //qDebug() << i.key() << ": " << i.value().name << " " << degreesToSeconds( i.value().longitude )<< endl; for(int j = 1; j <= 12; j++){ if(degreesToSeconds( i.value().longitude ) >= d1Border[j-1] && degreesToSeconds( i.value().longitude ) < d1Border[j]) { if(i.key() == kAsc){ rootD1Ascendant = j; } d1GrahaInSign.insert(i.key(), j); d1GrahaInHouse.insert(i.key(), howManyHousesAway(rootD1Ascendant, j)); QMap<QString, int> houseNumAndSign; //ASSERT that rootD1Ascendant is not 0 houseNumAndSign.insert(kHouseNum, howManyHousesAway(rootD1Ascendant, j)); houseNumAndSign.insert(kSignNum, j); Graha g = i.value(); g.chartMap.insert(kD1, houseNumAndSign); qDebug() << "***************************"; qDebug() << i.key()<< ":" << g.chartMap.value(kD1).value(kHouseNum); qDebug() << i.key() << ":" << g.chartMap.value(kD1).value(kSignNum); qDebug() << "**************************"; grahaMap[i.key()] = g; } } }
}
@