Results 1 to 2 of 2

Thread: best way to check for data type

  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Question best way to check for data type

    Hi,

    I was wondering what would be the best way using Qt to check for the input data type. Data input is done using QTextStream. I'd like to know if each data entry is a) an integer, b) a floating number, or c) a string. What would be the fastest and most reliable way to tell that.

    So far I've been doing it like this (but it is not elegant):
    Qt Code:
    1. //string_in is the string to be tested
    2. QString TestString = string_in; strcpy(string_in, TestString.toLocal8Bit().constData());
    3. int TestInt = TestString.toInt();
    4. float TestFloat = TestString.toFloat();
    5.  
    6. QString int_to_text; int_to_text.setNum(TestInt);
    7. char int_to_string[999]; strcpy(int_to_string, int_to_text.toLocal8Bit().constData());
    8.  
    9. if(strcmp(string_in,int_to_string)==0) datatype=1; //entry is int (1)
    10. else
    11. {
    12. QString float_to_text; float_to_text.setNum(TestFloat);
    13. char float_to_string[999]; strcpy(float_to_string, float_to_text.toLocal8Bit().constData());
    14. if(strcmp(string_in,float_to_string)==0) datatype=2;
    15.  
    16. else datatype=3; //entry is string (3)
    17. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!
    Last edited by timmu; 3rd January 2012 at 11:48.

  2. #2
    Join Date
    Jan 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: best way to check for data type

    You can use the boost/lexical_cast to convert your string and catch a bad_lexical_cast if convertion failed. That would be more elegant.

Similar Threads

  1. QList of variable data type?
    By Phlucious in forum Qt Programming
    Replies: 13
    Last Post: 2nd December 2011, 00:25
  2. QtSql data type mappings
    By eclarkso in forum Qt Programming
    Replies: 2
    Last Post: 16th December 2009, 17:22
  3. Using QVariant with custom data type
    By QAlex in forum Qt Programming
    Replies: 3
    Last Post: 4th December 2009, 12:04
  4. Data type parameters.
    By vinny gracindo in forum Newbie
    Replies: 1
    Last Post: 28th October 2009, 09:00
  5. Data type error
    By MrShahi in forum Qt Programming
    Replies: 1
    Last Post: 16th July 2008, 14:01

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.