Results 1 to 2 of 2

Thread: Index out of range problem for a xml file

  1. #1
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Index out of range problem for a xml file

    Hi all

    I have following code

    Qt Code:
    1. QStringList objectName_list;
    2. objectName_list<<"label_master_ac"<<"label_living_ac"<<"label_dining_ac"<<"label_bathroom_geyser"<<"label_entertainment"
    3. <<"label_heater";
    4.  
    5. QFile *file = new QFile("E:/label_with_xml_file/labelname.xml");
    6. if (!file->open(QFile::ReadOnly | QFile::Text)) {
    7. QMessageBox::warning(this, tr("Error: Cannot read file"),tr(qPrintable(file->errorString())));
    8. }
    9. doc.setContent(file);
    10. QDomElement root = doc.documentElement();
    11. QDomNode node = root.firstChild();
    12. i=0;
    13. while(!node.isNull())
    14. {
    15. if(node.toElement().tagName() == "label"){
    16. if(node.toElement().attribute("objectName") != "label_master_ac" &&
    17. node.toElement().attribute("objectName") != "label_living_ac" &&
    18. node.toElement().attribute("objectName") != "label_dining_ac" &&
    19. node.toElement().attribute("objectName") != "label_bathroom_geyser"
    20. && node.toElement().attribute("objectName") != "label_entertainment"
    21. && node.toElement().attribute("objectName") != "label_heater"){
    22.  
    23. qDebug()<<"while"<<node.toElement().attribute("objectName");
    24. node = node.nextSibling();
    25. continue;
    26. }
    27. qDebug()<<QString::number(i);
    28. QLabel *label = new QLabel(this);
    29. QFont font("Verdana", 10);
    30. font.setBold(true);
    31. label->setFont(font);
    32. label->setAlignment(Qt::AlignCenter);
    33. label->setFixedHeight(screenHeight/35);
    34. label->setStyleSheet("QLabel { background-color: rgb(240, 240, 240,255); color: rgb(131, 64, 0, 255);}");
    35. label->setObjectName(objectName_list.at(i));
    36. qDebug()<< node.toElement().attribute("objectName");
    37. if(objectName_list.at(i) == "label_master_ac" && node.toElement().attribute("objectName") == "label_master_ac"){
    38. label->setText(node.toElement().text());
    39. grid->addWidget(label, 0,1);
    40. }
    41. if(objectName_list.at(i) == "label_living_ac" && node.toElement().attribute("objectName") == "label_living_ac"){
    42. label->setText(node.toElement().text());
    43. grid->addWidget(label, 0,2);
    44. }
    45. if(objectName_list.at(i) == "label_dining_ac" && node.toElement().attribute("objectName") == "label_dining_ac"){
    46. label->setText(node.toElement().text());
    47. grid->addWidget(label, 0,3);
    48. }
    49. if(objectName_list.at(i) == "label_bathroom_geyser" && node.toElement().attribute("objectName") == "label_bathroom_geyser"){
    50. label->setText(node.toElement().text());
    51. grid->addWidget(label, 4, 1);
    52. }
    53. if(objectName_list.at(i) == "label_entertainment" && node.toElement().attribute("objectName") == "label_entertainment"){
    54. label->setText(node.toElement().text());
    55. grid->addWidget(label, 4, 2);
    56. }
    57. if(objectName_list.at(i) == "label_heater" && node.toElement().attribute("objectName") == "label_heater"){
    58. label->setText(node.toElement().text());
    59. grid->addWidget(label, 4, 3);
    60. }
    61. }
    62. node = node.nextSibling();
    63. i++;
    64. file->seek(0);
    65. }
    To copy to clipboard, switch view to plain text mode 

    On running it gives following error msg

    ASSERT failure in QList<T>::at: "index out of range", file c:\QtSDK1\Desktop\Qt\4.7.3\mingw\include/QtCore/qlist.h, line 456
    Invalid parameter passed to C runtime function.
    Invalid parameter passed to C runtime function.

    I know that i am accessing a value which is out of range, but i do not not understand why?
    Can anybody suggest me?
    Thanks

  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: Index out of range problem for a xml file

    Because in line 64 i is incremented, but never is checked if i < objectName_list.size() and node have more elements than objectName_list.

Similar Threads

  1. Replies: 3
    Last Post: 27th July 2011, 09:30
  2. Replies: 6
    Last Post: 8th April 2011, 21:15
  3. Handling "index out of range"
    By liqxpil in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2010, 23:25
  4. Index out of Range
    By santhoshv84 in forum Qt Programming
    Replies: 2
    Last Post: 19th August 2008, 15:33
  5. QList index out of range problem
    By MarkoSan in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2008, 08:40

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.