Results 1 to 7 of 7

Thread: QT newbie: QMapIterator not showing any output, but foreach does?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QT newbie: QMapIterator not showing any output, but foreach does?

    QT4.8.1

    The following is a code snippet. It reads in a text file where each line contains json in a non-pretty-print format.

    The intent is to read in each line, extract a key called "prog_id", then store a QMap using prog_id as the key.

    Qt Code:
    1. QString filename = "/tmp/10021_sched.txt";
    2.  
    3. QFile inputfile(filename);
    4. if (!inputfile.open(QIODevice::ReadOnly))
    5. {
    6. qDebug() << "Couldn't open filename: " << filename;
    7. return false;
    8. }
    9.  
    10. QJson::Parser parser;
    11. bool ok;
    12.  
    13. QMap<QString, QString> map;
    14. QMapIterator<QString, QString> i(map);
    15. QString line;
    16. QString key;
    17.  
    18. while (!inputfile.atEnd())
    19. {
    20. // Read everything into a QMap first. The largest data file only has around 4000 lines, so not huge.
    21. line = inputfile.readLine();
    22. QVariantMap result = parser.parse(line.toLocal8Bit(), &ok).toMap(); //json parser wants QByteArray
    23. if (!ok)
    24. {
    25. printf("line %d: %s\n", parser.errorLine(), parser.errorString().toUtf8().data());
    26. return false;
    27. }
    28.  
    29. // Store each line read in from the file using prog_id as the key.
    30. //qDebug() << "progid is " << result["prog_id"];
    31. //qDebug() << "line is " << line;
    32.  
    33. key = result["prog_id"].toString();
    34.  
    35. // The next line works.
    36. qDebug() << "key is: " << key;
    37.  
    38. map[key] = line;
    39. }
    40. inputfile.close();
    41.  
    42. // Added this because the iterator didn't seem to work.
    43. i.toFront();
    44.  
    45. // This prints.
    46. printf("Starting loop\n");
    47.  
    48. while (i.hasNext())
    49. {
    50. // This doesn't print.
    51. printf("Inside loop\n");
    52. i.next();
    53. // I never get any output inside the while
    54. qDebug() << i.key() << ": " << i.value();
    55. }
    56.  
    57. // This works.
    58. foreach (QString value, map)
    59. {
    60. qDebug() << "value is: " << value;
    61. }
    To copy to clipboard, switch view to plain text mode 

    Snippet is obviously kludged together, but I'm not understanding why the while "Inside Loop" never seems to execute.
    Last edited by wysota; 31st May 2012 at 22:42. Reason: missing [code] tags

Similar Threads

  1. Day showing wrongly in thread list in NewBie
    By karthic in forum General Discussion
    Replies: 1
    Last Post: 29th April 2012, 14:06
  2. lifetime of foreach
    By BalaQT in forum Newbie
    Replies: 4
    Last Post: 4th March 2010, 15:55
  3. bad behaviour of QMapIterator
    By zorro68 in forum Qt Programming
    Replies: 2
    Last Post: 14th February 2008, 22:16
  4. Showing the output from cmake on my widget
    By jsmax in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2007, 20:17
  5. Replies: 4
    Last Post: 10th May 2006, 22:37

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.