Results 1 to 7 of 7

Thread: Help with parsing a QString

  1. #1
    Join Date
    Feb 2015
    Posts
    21
    Thanks
    2

    Default Help with parsing a QString

    Hey folks.

    Consider i have this QString "EMU 978 EUR 1 7,537897 7,560579 7,583261"

    and i need to use the 7,560579 column for further calculations.

    any suggestions on how to do it properly?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with parsing a QString

    Read about QString::split

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

    bikonja (7th June 2015)

  4. #3
    Join Date
    Feb 2015
    Posts
    21
    Thanks
    2

    Default Re: Help with parsing a QString

    thx, that helped. now if i can trouble you some more

    so i have this parser class im making

    Qt Code:
    1. #include <QFile>
    2. #include <QTextStream>
    3. #include <QString>
    4. #include <QTextStream>
    5. #include <QMessageBox>
    6. #include <QDebug>
    7. #include <QStringList>
    8. class parser : public QObject
    9.  
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit parser(QObject *parent = 0);
    14.  
    15. void process_line(QString line);
    16. int read_line();
    17. void init_map();
    18. void init_list();
    19.  
    20. private:
    21. QMap<QString,float> map;
    22. QStringList valute;
    23. };
    To copy to clipboard, switch view to plain text mode 

    and the .cpp

    Qt Code:
    1. #include "parser.h"
    2.  
    3. parser::parser(QObject *parent) :
    4. QObject(parent)
    5. {
    6. init_map();
    7. init_list();
    8. }
    9. void parser::process_line(QString line)
    10. {
    11. QString str="EUR";
    12. QStringList temporary_list;
    13. if(line.contains(str,Qt::CaseInsensitive)) this is the part im having trouble with now**
    14. {
    15. qDebug()<<"found euro";
    16. temporary_list=line.split(" ", QString::SkipEmptyParts);
    17. map.insert("EUR",temporary_list[6].toFloat());
    18.  
    19. }
    20.  
    21. }
    22. int parser::read_line()
    23. {
    24.  
    25. QFile file("C:/downloaded.txt");
    26.  
    27. if(!file.exists())
    28. {
    29. QMessageBox msgBox;
    30. msgBox.setText("There is no such file");
    31. msgBox.exec();
    32. return 1;
    33. }
    34.  
    35. if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    36. {
    37. QMessageBox msgBox;
    38. msgBox.setText("Error while opening file");
    39. return 1;
    40. }
    41.  
    42. QTextStream in_stream(&file);
    43. QString line=in_stream.readLine();
    44.  
    45.  
    46. while (!line.isNull())
    47. {
    48. process_line(line);
    49. line = in_stream.readLine();
    50. }
    51.  
    52.  
    53. return 0;
    54. }
    55.  
    56. void parser::init_map()
    57. {
    58. map.insert("AUD",0);
    59. map.insert("CAD",0);
    60. map.insert("CZK",0);
    61. map.insert("DKK",0);
    62. map.insert("NOK",0);
    63. map.insert("SEK",0);
    64. map.insert("CHF",0);
    65. map.insert("GBP",0);
    66. map.insert("USD",0);
    67. map.insert("EUR",0);
    68. map.insert("PLN",0);
    69. }
    70.  
    71. void parser::init_list()
    72. {
    73. valute << "AUD" << "CAD" << "CZK" << "DKK" << "NOK" << "SEK"
    74. << "CHF" << "GBP" << "USD" << "EUR" << "PLN";
    75. }
    To copy to clipboard, switch view to plain text mode 

    i have a txt file of over 60 lines from which i have to read the values.
    so i would like my process_line function to check the QString it receives if there is any matching strings from my QStringlist in there.
    and if so to insert the value to my map.

    im not sure how to write that in my if statement, and im pretty sure also i have the wrong aproach here. so if you could point me in the right direction again, would be very much appreciated.

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with parsing a QString

    But what is a problem ?

  6. #5
    Join Date
    Feb 2015
    Posts
    21
    Thanks
    2

    Default Re: Help with parsing a QString

    i dont know how the write the loop.

    i did this one with checking only for "EUR" in my line.

    to be more precise with some pseudo coding

    if(line contains any of the strings from my value QStringList)
    split the line into a new qstringlist
    instert value from index no.6 to the matching key/value pair in my map

  7. #6
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Help with parsing a QString

    QStringList::contains()
    QStringList::indexOf()

  8. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with parsing a QString

    Simply :
    Qt Code:
    1. void parser::process_line(QString line)
    2. {
    3. str << "EUR" << "USD" << "GBP";
    4. for( int i = 0; i < str.size(); i++ )
    5. {
    6. if(line.contains(str.at(i),Qt::CaseInsensitive))
    7. {
    8. QStringList temporary_list;
    9. qDebug()<<"found " << str.at(i);
    10. temporary_list=line.split(" ", QString::SkipEmptyParts);
    11. map.insert(str.at(i)",temporary_list[6].toFloat());
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 3
    Last Post: 27th December 2013, 18:04
  2. Replies: 3
    Last Post: 27th July 2012, 09:30
  3. Replies: 2
    Last Post: 11th August 2011, 15:42
  4. Replies: 4
    Last Post: 1st February 2010, 14:21
  5. Replies: 4
    Last Post: 31st January 2008, 20:44

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.