Results 1 to 9 of 9

Thread: Can't read map data

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

    Default Can't read map data

    Hello, i have a problem with reading my map data or possibly even writing it in.

    the app i'm making is a basic currency converter.

    I download the exchange rates from a website, save it into txt file, which i then parse to save the values into a map and use my calculator class to do the conversion.

    the downloader class works flawless (i got it of offical QT forums) so i wont post it since the problem aint there.

    the code: 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. downloader d;
    8. d.Do_download();
    9.  
    10. parser p;
    11. p.read_line(currency_map);
    12.  
    13. p.print_map(currency_map);// this line works, and it prints out the map
    14.  
    15. MainWindow w(currency_map);
    16. w.show();
    17.  
    18. return a.exec();
    19. };
    To copy to clipboard, switch view to plain text mode 
    parser.cpp
    im pretty sure it works well because the print_map function does its job.

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

    now i have a calculator class :

    .h

    Qt Code:
    1. class Calculator
    2. {
    3. public:
    4. explicit Calculator(QMap<QString,double> &currency_map);
    5. void multiply();
    6. void getValues(QString strFrom, QString strTo);
    7. double getTotal();
    8. private:
    9. double total, firstCurr, secondCurr;
    10. QMap<QString,double> &map;
    11.  
    12. };
    To copy to clipboard, switch view to plain text mode 

    .cpp

    Qt Code:
    1. #include "calculator.h"
    2.  
    3. Calculator::Calculator(QMap<QString,double> &currency_map):map(currency_map)
    4. {
    5. total = 0;
    6. firstCurr = 0;
    7. secondCurr= 0;
    8. }
    9.  
    10. void Calculator::getValues(QString strFrom, QString strTo)
    11. {
    12. QMap<QString, double>::iterator i;
    13. for(i=map.begin();i!=map.end();i++);
    14. {
    15.  
    16. if(!i.key().compare(strFrom))
    17. firstCurr=i.value();
    18. if(!i.key().compare(strFrom))
    19. secondCurr = i.value();
    20. }
    21. //firstCurr = 2;
    22. //secondCurr = 3;
    23. }
    24.  
    25. void Calculator::multiply()
    26. {
    27. total = firstCurr * secondCurr;
    28. }
    29.  
    30. double Calculator::getTotal()
    31. {
    32. return total;
    33. }
    To copy to clipboard, switch view to plain text mode 

    then i create a Calculator object in my mainWindow class
    .h

    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MainWindow(QMap<QString,double> &currency_map, QWidget *parent = 0);
    7.  
    8. ~MainWindow();
    9.  
    10.  
    11. private slots:
    12. void on_convert_button_clicked();
    13.  
    14. private:
    15. Ui::MainWindow *ui;
    16. Calculator calc;
    17.  
    18. };
    To copy to clipboard, switch view to plain text mode 

    .cpp

    Qt Code:
    1. MainWindow::MainWindow(QMap<QString, double> &currency_map, QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow),calc(currency_map)
    4. {
    5. ui->setupUi(this);
    6. }
    7.  
    8. MainWindow::~MainWindow()
    9. {
    10. delete ui;
    11. }
    12.  
    13. void MainWindow::on_convert_button_clicked()
    14. {
    15. calc.getValues(ui->from_Combox->currentText(),ui->to_Combox->currentText());
    16. calc.multiply();
    17. ui->lcdNumber->display(calc.getTotal());
    18. }
    To copy to clipboard, switch view to plain text mode 

    but i cant seem to get any values from the map.
    the wierd thing also is when i debugg(i use visual studio, have some trouble setting up a debbuger in QT Creator) its always shows the map as empty, which i cant grasp since the print function works.

    any help would be appreciated. thx

  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: Can't read map data

    Quote Originally Posted by bikonja View Post
    Qt Code:
    1. for(i=map.begin();i!=map.end();i++);
    2. {
    3. /// ...
    4. }
    To copy to clipboard, switch view to plain text mode 
    You have a for loop without body, followed by a block that operates on an invalid iterator.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't read map data

    On top of that, getValues() does not use its strTo parameter. I suppose that the compiler warns about it, doesn't it? By the way, the whole point of QMap is that you do not have to traverse the whole collection to find the value mapped to a key.

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

    Default Re: Can't read map data

    Quote Originally Posted by anda_skoa View Post
    You have a for loop without body, followed by a block that operates on an invalid iterator.

    Cheers,
    _
    check again

    Quote Originally Posted by yeye_olive View Post
    On top of that, getValues() does not use its strTo parameter. I suppose that the compiler warns about it, doesn't it? By the way, the whole point of QMap is that you do not have to traverse the whole collection to find the value mapped to a key.
    yes, that was a brain lapse. it was the wrong parameter.

    i appreciate the hints, and i will use them but i still don't know why i can't read my map values.

  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't read map data

    Quote Originally Posted by bikonja View Post
    check again
    anda_skoa is right. Look at the last character of the line with 'for'.

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

    Default Re: Can't read map data

    Okey, you got me there. I was looking at the wrong place. (didnt solve my problem tho)
    But still, i dont know why my debugger says the map is empty, and if it actually is empty, where did my print function get the data it writes out.
    an example would be
    debugger: (error), 0
    print_map_"AUD" : 5.13678 and 11 more lines of different examples

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can't read map data

    yeye_olive is right - you don't seem to know how maps are used. The whole point of a map is that you can write something like:

    Qt Code:
    1. firstCurr = currencyMap[ strFrom ];
    To copy to clipboard, switch view to plain text mode 

    You don't need to iterate over the whole map and examine everything in it. If you want to make sure the key is actually in there, you can check QMap::contains() first.

    Other than the extra ";" that you should have removed by now (in fact, the whole for() loop needs to go), I don't see anything obviously wring with the code. You do know that the default for QString::compare() is Qt::CaseSensitive, right, which means that the currency names you are getting from your line edits better match the values in your map exactly?

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

    Default Re: Can't read map data

    Thx alot for the tips. I dont really know why i was iterating the map, i'm aware of its functionality

    And yes all my strings are in capital letters, so that wont be a problem.

    Is it possible that visual studio can't read my QT objects and that's why the debugger shows that my map is empty?

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can't read map data

    Is it possible that visual studio can't read my QT objects and that's why the debugger shows that my map is empty?
    The VS debugger works just fine looking into Qt objects. You might have to click through a couple of levels of indirection, since most Qt classes are implemented using a private pointer to the true contents (Envelope - Letter paradigm).

    Have you simply put a qDebug() statement inside the constructors for your MainWindow and Calculator classes to see what the map looks like there?

    Qt Code:
    1. qDebug() << currency_map;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Serial read misses to read data from the serial port
    By mania in forum Qt for Embedded and Mobile
    Replies: 11
    Last Post: 18th August 2014, 08:49
  2. read data from website
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 4
    Last Post: 27th June 2013, 11:32
  3. Read data from txt file
    By wiatrak11 in forum Newbie
    Replies: 4
    Last Post: 10th March 2013, 16:50
  4. read output data
    By KillGabio in forum General Programming
    Replies: 4
    Last Post: 6th February 2012, 16:31
  5. can`t read a binary data!
    By blm in forum Qt Programming
    Replies: 8
    Last Post: 18th September 2008, 16:56

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.