Results 1 to 2 of 2

Thread: Trouble with QMap

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

    Default Trouble with QMap

    Hello, i got a question.

    Consider the following code

    parser.h

    Qt Code:
    1. class parser : public QObject
    2.  
    3. {
    4. Q_OBJECT
    5. public:
    6. explicit parser(QObject *parent = 0);
    7.  
    8. void process_line(QString line, QMap<QString,double> my_map);
    9. int read_line(QMap<QString,double> my_map);
    10. void print_map(QMap<QString,double> my_map);
    11.  
    12. };
    To copy to clipboard, switch view to plain text mode 

    parser.cpp

    Qt Code:
    1. void parser::process_line(QString line, QMap<QString, double> my_map)
    2. {
    3. QStringList temporary_list;
    4.  
    5. for(int i = 0; i< currency_list.size();i++)
    6. {
    7. if(line.contains(currency_list.at(i),Qt::CaseInsensitive))
    8. {
    9. qDebug()<<"found"<< currency_list.at(i);
    10. temporary_list=line.split(" ",QString::SkipEmptyParts);
    11. qDebug()<< temporary_list[6];
    12. temporary_list.replaceInStrings(",",".");
    13. my_map.insert(currency_list.at(i),temporary_list[6].toDouble());
    14. }
    15. }
    16.  
    17. }
    18.  
    19. int parser::read_line(QMap<QString, double> my_map)
    20. {
    21.  
    22. QFile file("C:/Qt/test/downloaded.txt");
    23.  
    24. if(!file.exists())
    25. {
    26. QMessageBox msgBox;
    27. msgBox.setText("There is no such file");
    28. msgBox.exec();
    29. return 1;
    30. }
    31.  
    32. if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    33. {
    34. QMessageBox msgBox;
    35. msgBox.setText("Error while opening file");
    36. msgBox.exec();
    37. return 1;
    38. }
    39.  
    40. QTextStream in_stream(&file);
    41. QString line=in_stream.readLine();
    42.  
    43.  
    44. while (!line.isNull())
    45. {
    46. process_line(line, my_map);
    47. line = in_stream.readLine();
    48. }
    49.  
    50.  
    51. return 0;
    52. }
    53.  
    54. void parser::print_map(QMap<QString, double> my_map)
    55. {
    56. QMapIterator<QString, int> i(map);
    57. while (i.hasNext()) {
    58. i.next();
    59. qDebug()<< i.key() << ": " << i.value() << endl;
    60. }
    To copy to clipboard, switch view to plain text mode 

    i tried this to test for data.

    Qt Code:
    1. void parser::print_map(QMap<QString, double> my_map)
    2. {
    3. QMapIterator<QString, int> i(map);
    4. if(!i.hasNext()) {
    5. qDebug()<< "nothing here" << endl;
    6. }
    To copy to clipboard, switch view to plain text mode 

    and it shows that nothing is there

    and in my main

    main.cpp

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. QMap<QString,double> currency_map;
    6.  
    7. MainWindow w;
    8. w.show();
    9.  
    10. parser p;
    11. p.read_line(currency_map);
    12.  
    13. p.print_map(currency_map);
    14.  
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 


    What am i doing wrong while trying to print (and maybe even saving) keys/values in my map?
    my read_line() works ok, qdebugger is showing everything and im also under the impression that my process_line works ok as well.

    I need to use the map in other classes as well, so im thinking it would be most painless to make it in my main and send it around.
    But im obviously missing something here, would appreciate it if you could help me out.

    Thx

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Trouble with QMap

    You are passing a copy of the map into read_line() And process_line(). Each function modifies its own copy and then the copy is discarded at the end of the function. Pass the map by non-const reference if you want to modify the passed map.

Similar Threads

  1. Problem With QMap
    By alizadeh91 in forum Qt Programming
    Replies: 7
    Last Post: 12th March 2013, 07:14
  2. QMap
    By sophister in forum Qt Programming
    Replies: 5
    Last Post: 25th May 2009, 10:05
  3. QMap
    By AnithaRagupathy in forum Qt Programming
    Replies: 5
    Last Post: 23rd November 2007, 13:26
  4. Qmap
    By phillip_Qt in forum Qt Programming
    Replies: 3
    Last Post: 23rd November 2007, 10:43
  5. Reg - QMap(Qt3.3.4)
    By suresh in forum Newbie
    Replies: 3
    Last Post: 18th October 2006, 22:04

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.