Hi everyone,

In my program I have to set a variable by reading a ini file, the problem is the variable can be either string or double, and I need to assign them to the correct variable type.

To be more precise, my ini file will look like this:

[DATA]
link0 theta=t1
link0 alfa=a1

link1 theta=0.0
link1 alfa=a2

link3 theta=0
link3 alfa=90

The variables "theta" and "alpha" can be defined as a string ("a1", "t1") or as a double ("0.0","90").

if I do something like this:

Qt Code:
  1. settings.beginGroup("DATA");
  2. const QStringList childKeys = settings.childKeys();
  3.  
  4. foreach (const QString &childKey, childKeys)
  5. {
  6. std::cout << settings.value(childKey).type() << std::endl;
  7. std::cout << settings.value(childKey).toString().toStdString() << std::endl;
  8. }
  9. settings.endGroup();
To copy to clipboard, switch view to plain text mode 

the type() function always return the value=10, which in QVariant enum Type means "string", even when the variable is a number (as in the case of "theta" of link1 =0.0 )

What I need to do is, if the data in the ini File is a number store it in a double variable, if is a string then store it in a string variable. For example, in my example of ini File:
the variable "theta" of link0 should be stored in a string--> std::string a= settings.value(childKey).toString().toStdString()
the variable "theta" of link1 should be stored in a double--> double b= settings.value(childKey).toDouble()

This is:
Qt Code:
  1. foreach (const QString &childKey, childKeys)
  2. {
  3. std::cout << settings.value(childKey).type() << std::endl;
  4. if (type is number) //<<<<<<<<<<<<<<<<<<<<<< How to detect the correct type here?
  5. double b= settings.value(childKey).toDouble()
  6. else
  7. std::string a= settings.value(childKey).toString().toStdString()
  8. }
To copy to clipboard, switch view to plain text mode 


I have in mind to treat all the variables as a string and detect if in the string there's any of the characters "a","b","c",...,"x","y","z","A","B",...,"Z", if there is any of them, then saved in a string variable, else is a number. However, that sounds to complicated. Is that possible in a simple way?

I Hope the question is not to confusing .

Thanks,