Results 1 to 3 of 3

Thread: Edit Feature works perfect if I do not change index...

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Cool Edit Feature works perfect if I do not change index...

    I am not sure what I did wrong..but it only works 100% if say the first item I edited was index 1. then The second item I want to edit would also have to be index 1 if the second item I try to edit is index 0... it Will not work it will basically not take in the info that is supposed to be there.
    here is the code:
    Qt Code:
    1. void DateListDialog::updateTreeWidget(){
    2. for (int i = 0; i < dates.size(); ++i) {
    3.  
    4. itemParent = new QTreeWidgetItem(treeWidget);
    5. for(int j = 0; j<24; j++){
    6. timeChild[j] = new QTreeWidgetItem(itemParent);
    7. //timeChild[j]->setFlags(Qt::NoItemFlags);
    8. }
    9.  
    10. itemParent->setText(1, foods[i]);
    11. for(int k = 0; k<7; k++){
    12. itemParent->setText(k+2, totalValues[k+(7*i)]);
    13. //cout << totalValues[k+(7*i)].toStdString() << endl;
    14. }
    15.  
    16. itemParent->setText(0, dates[i]);
    17.  
    18. for(int j = 0; j<24; j++){
    19. timeChild[j]->setText(1, times[j+(24*i)]);
    20. }
    21.  
    22. for(int i = 0; i<24; i++){
    23. if(i == 0){
    24. time = "12 AM";
    25. } else if(i > 0 && i < 12){
    26. time = QString("%1 AM").arg(i);
    27. } else if(i == 12){
    28. time = "12 PM";
    29. } else if(i > 12){
    30. time = QString("%1 PM").arg(i-12);
    31. }
    32. timeChild[i]->setText(0, time);
    33.  
    34. }
    35.  
    36. treeWidget->addTopLevelItem(itemParent);
    37.  
    38. }
    39.  
    40.  
    41. allDates << dates;
    42. allTimes << times;
    43. /*for(int i = 0; i<allDates.size();i++){
    44.   for(int j = 0; j<24; j++){
    45.   if(!allTimes[j+(24*i)].contains("No Food")){
    46.   cout << allTimes[j+(24*i)].toStdString() << flush;
    47.   }
    48.   }
    49.   cout << endl;
    50.   }*/
    51. allFoods << foods;
    52. allValues << totalValues;
    53.  
    54. }
    55. void DateListDialog::editDate()
    56. {
    57. dates.clear();
    58. times.clear();
    59. foods.clear();
    60. totalValues.clear();
    61. for(int i = 0; i<24;i++){
    62. addedTimes[i].clear();
    63. }
    64. edit = true;
    65. for(int i = 0; i<24; i++){
    66. if(!allTimes[i].contains("No Food")){
    67. addedTimes[i] = allTimes[i];
    68. }
    69. for(int i = 0; i<7; i++){
    70. totalValue[i] = allValues[i].toInt();
    71. }
    72. }
    73. posInt = treeWidget->indexOfTopLevelItem(treeWidget->currentItem());
    74. ListWidgetDialog w;
    75. w.exec();
    76. if(changed == true){
    77. for(int i = 23; i>=0; i--){
    78. allTimes.removeAt(i+(24*posInt));
    79. }
    80. allDates.removeAt(posInt);
    81. allFoods.removeAt(posInt);
    82. for(int i = 6; i>=0; i--){
    83. allValues.removeAt(i+(7*posInt));
    84. }
    85. // cout << "Changed = true" << endl;
    86. treeWidget->takeTopLevelItem(posInt);
    87. updateTreeWidget();
    88. }
    89. edit = false;
    90. /* for(int i = 0; i<allTimes.size(); i++){
    91.   if(!allTimes[i].contains("No Food")){
    92.   cout << allTimes[i].toStdString() << endl;
    93.   }
    94.   }*/
    95. }
    96. void DateListDialog::treeWidget_clicked(){
    97. //cout << "HELLO" << endl;
    98. }
    99. void DateListDialog::deleteDate()
    100. {
    101. posInt = treeWidget->indexOfTopLevelItem(treeWidget->currentItem());
    102. if(posInt >= 0){
    103. treeWidget->takeTopLevelItem(posInt);
    104. for(int i = 23; i>=0; i--){
    105. allTimes.removeAt(i+(24*posInt));
    106. //cout << i+(24*posInt) << endl;
    107. }
    108. //cout << "SELECTED POSITION IS: " << posInt << endl;
    109. allDates.removeAt(posInt);
    110. allFoods.removeAt(posInt);
    111. for(int i = 6; i>=0; i--){
    112. allValues.removeAt(i+(7*posInt));
    113. }
    114.  
    115. QFile file(QDir::homePath() + "/datedata.txt");
    116. if (file.open(QIODevice::WriteOnly)) {
    117. QTextStream out(&file);
    118. out.setCodec("UTF-8");
    119. for(int i = 0; i<allDates.size(); i++){
    120. out << allDates[i] << endl;
    121. for(int j = 0; j<24; j++){
    122. out << allTimes[j+(24*i)] << endl;
    123. }
    124. out << allFoods[i] << endl;
    125. for(int k = 0; k<7; k++){
    126. allValues[k+(7*i)];
    127. }
    128.  
    129. out << endl;
    130. }
    131. }
    132. file.close();
    133. }
    134. //cout << "SIZE AFTER WRITE: " << allDates.size() << endl;
    135.  
    136.  
    137. }
    To copy to clipboard, switch view to plain text mode 

    I think it would work perfect if...I could edit the old one with out removing them. how would I go about updating my QTreeWidget via a QDialog
    I am going to keep trying to figure it out while I wait but any advice/suggestions would be appreciated =]

    EDIT:::EDIT::
    argh...the reason it wasnt working correctly was because instead of
    Qt Code:
    1. if(!allTimes[i].....);
    2. //was suppsed to be
    3. if(!allTimes[i+(24*posInt)]......)
    To copy to clipboard, switch view to plain text mode 

    I would still like to know if there is a more efficient way that would be to just edit the old item and update instead of editing old item, removing it, then reinserting at the end.
    Last edited by giblit; 30th March 2013 at 03:51.

Similar Threads

  1. How to change QStackedWidget index from Qdialog
    By sliverTwist in forum Qt Programming
    Replies: 5
    Last Post: 20th March 2013, 19:24
  2. Change QVBoxLayout index?
    By davidlamhauge in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2012, 00:51
  3. Change QTableView's index number displayed
    By rex in forum Qt Programming
    Replies: 0
    Last Post: 6th April 2011, 15:30
  4. Replies: 4
    Last Post: 2nd March 2011, 09:03
  5. Replies: 8
    Last Post: 15th May 2007, 09:21

Tags for this Thread

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.