0

I have a QMap and I have defined it as QMap<QString,ModelVariables>ModelMap where ModelVariables is a structure with parameters valueref,value and ID. I want to access the parameter value defined in the structure. I wrote the following code but it shows an error.

float hxt_val_ft04 = ModelMap.value("HXT_V_FT04");

ERROR: expected primary expression before '.' token.

Karup
  • 2,024
  • 3
  • 22
  • 48

2 Answers2

0

MVariables["HXT_V_FT04"] would return a ModelVariables object if there is a key matching "HXT_V_FT04". Otherwise it will default construct that object and return it. so you can use

MVariables["HXT_V_FT04"].value
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
0

Presuming "HXT_V_FT04" is your key, the value will be a ModelVariables struct. You can access that struct either with square brackets, which will create a value if it doesn't exist, or use .at(). For example

if(ModelMap.contains("HXT_V_FT04"))
{
  ModelVariables mystruct = ModelMap.at("HXT_V_FT04");
  float hxt_val_ft04 = mystruct.value;
}

This is the same as

if(ModelMap.contains("HXT_V_FT04"))
  float hxt_val_ft04 =ModelMap.at("HXT_V_FT04").value;

Alternatively:

// create mystruct if it doesn't exist
ModelVariables mystruct = ModelMap.at["HXT_V_FT04"];
float hxt_val_ft04 = mystruct.value;

Edit: Based on the typenames etc you have provided, I think you need something that looks like this:

// declare the map
Map<QString,ModelVariables> ModelMap;
ModelVariables MVar;

QString key = "HXT_V_FT04";
// assign to Mvar.value
MVar.value = 10.0;

// insert into the map
ModelMap.insert(key, MVar);

// do something

// read back the value in MVar
float val = ModelMap.at(key).value;
//or 
float val = ModelMap[key].value;
//or
float val = ModelMap["HXT_V_FT04"].value;
// or 
ModelVariables mystruct = ModelMap["HXT_V_FT04"];
float val = mystruct.value;
mike
  • 1,192
  • 9
  • 32
  • Can you post code showing how you insert the ModelVariables into the map, and then attempt to access it? – mike Jun 26 '15 at 11:21
  • QMap ModelMap; ModelMap is defined as a QMap. The object declared is ModelMap MVariables. ModelVariables is a structure with three parameters value,valueref and ID. The object declared for the structure is MVar. – Harish Ramamurthy Jun 26 '15 at 11:34
  • That's declaring ModelMap. To insert into ModelMap, you should have some code like ModelMap.insert("HXT_V_FT04", MVar); – mike Jun 26 '15 at 11:36
  • A simulation code is there. After the simulation is run, the value is assigned to the value parameter in structure. – Harish Ramamurthy Jun 26 '15 at 11:39
  • I'm asking to see code showing how you are inserting the ModelVariables structure MVar into the map. I'll post some example code above. – mike Jun 26 '15 at 11:40
  • I can understand ur point. There is a simulation of a plant in modelica software and it is integrated in Qt. When I run the simulation, it is processed and values are displayed across each parameter namely Key,value,valueref and ID. After the simulation is stopped a signal is emitted to update the object of the map. emit(updatevariables(Mvariables)) – Harish Ramamurthy Jun 26 '15 at 11:55
  • OK. But can you post the actual code so we can take a look please? There may be something quite obvious which is apparent when the code is visible. So far you have shown only the declaration of the map and the incorrect access. If you could show the exact code to insert into the map and the code you are now unable to compile, it would help us answer your question – mike Jun 26 '15 at 12:01
  • I am sorry. I am not allowed to do that and it is way too big to post it here. Following the method suggested by UmNyobe I get no errors but have to test it whether I get the final output. – Harish Ramamurthy Jun 26 '15 at 12:23