Results 1 to 3 of 3

Thread: How to automatically detect the type of a key in a ini file QSettings

  1. #1
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to automatically detect the type of a key in a ini file QSettings

    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,

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to automatically detect the type of a key in a ini file QSettings

    You could try
    Qt Code:
    1. if (settings.value(key).canConvert<double>()) // double
    To copy to clipboard, switch view to plain text mode 

    or use the boolean argument of toDouble() to check for conversion success
    Qt Code:
    1. bool ok = false;
    2. double d = settings.value(key).toDouble( &ok );
    3. if (!ok) // not a double
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    dean76 (16th January 2013)

  4. #3
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to automatically detect the type of a key in a ini file QSettings

    Thanks for the fast reply!

    I tried both and the second option is the best . Just to summarize:
    Given the next ini File:

    [GENERALCONFIG]
    DOF=2
    [DH]
    link0 theta= 0
    link0 d= d1
    link0 a = 34.456768996
    link0 alpha = alpha1

    link1 theta= 90
    link1 d= d2
    link1 a = a2
    link1 alpha = alpha2

    Where the variables can be either strings or doubles, the solution to store each variable in the correct data type is:

    Qt Code:
    1. settings.beginGroup("DH");
    2. const QStringList childKeys = settings.childKeys();
    3.  
    4. std::vector<double> vDouble;
    5. std::vector<std::string> vString;
    6.  
    7. foreach (const QString &childKey, childKeys)
    8. {
    9. bool ok = false;
    10. double d = settings.value(childKey).toDouble(&ok);
    11. if (ok) // a double
    12. {
    13. vDouble.push_back(d);
    14. std::cout << "Is a Double" << d << std::endl;
    15. }
    16. else
    17. {
    18. std::string temp;
    19. temp=settings.value(childKey).toString().toStdString();
    20. vString.push_back(temp);
    21. std::cout << "Is a String: " << temp << std::endl;
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    This is assuming that int and doubles need to be stored in the same variable type (double).

    Thanks!

Similar Threads

  1. Can QT application detect which type of connection is used?
    By satya@sipl in forum Qt Programming
    Replies: 1
    Last Post: 1st December 2012, 13:16
  2. How can we create .pc file automatically?
    By learning_qt in forum Qt Programming
    Replies: 6
    Last Post: 9th December 2010, 05:19
  3. Replies: 4
    Last Post: 3rd August 2010, 07:17
  4. Replies: 1
    Last Post: 7th March 2010, 16:40
  5. QSettings and new QVariant Type
    By baray98 in forum Qt Programming
    Replies: 7
    Last Post: 11th February 2008, 11:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.