dean76
15th January 2013, 15:52
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:
settings.beginGroup("DATA");
const QStringList childKeys = settings.childKeys();
foreach (const QString &childKey, childKeys)
{
std::cout << settings.value(childKey).type() << std::endl;
std::cout << settings.value(childKey).toString().toStdString() << std::endl;
}
settings.endGroup();
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:
foreach (const QString &childKey, childKeys)
{
std::cout << settings.value(childKey).type() << std::endl;
if (type is number) //<<<<<<<<<<<<<<<<<<<<<< How to detect the correct type here?
double b= settings.value(childKey).toDouble()
else
std::string a= settings.value(childKey).toString().toStdString()
}
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,
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:
settings.beginGroup("DATA");
const QStringList childKeys = settings.childKeys();
foreach (const QString &childKey, childKeys)
{
std::cout << settings.value(childKey).type() << std::endl;
std::cout << settings.value(childKey).toString().toStdString() << std::endl;
}
settings.endGroup();
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:
foreach (const QString &childKey, childKeys)
{
std::cout << settings.value(childKey).type() << std::endl;
if (type is number) //<<<<<<<<<<<<<<<<<<<<<< How to detect the correct type here?
double b= settings.value(childKey).toDouble()
else
std::string a= settings.value(childKey).toString().toStdString()
}
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,