Results 1 to 3 of 3

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

  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 04:51.

  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: Edit Feature works perfect if I do not change index...

    You can always access already created items. Top level items through QTreeWidget::topLevelItem() and any child items through QTreeWidgetItem::child() of their respective parent.

    Alternatively you can provide the data through a model and let a QTreeView deal with the visualization.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    giblit (5th April 2013)

  4. #3
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

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

    Sorry about coming back here after so much time but thanks for the reply =] I just recoded my add menu so the user can input their own times dynamically and now this will be very useful I just did what you tried and got what I was looking for =]
    Qt Code:
    1. for(int i = 0; i<9; i++){
    2. cout << "Top Level Item " << i << ": " << treeWidget->topLevelItem(treeWidget->indexOfTopLevelItem(treeWidget->currentItem()))->text(i).toStdString() << endl;
    3. for(int j = 0; j<treeWidget->currentItem()->childCount(); j++){
    4. cout << "Child " << i << ": " << treeWidget->currentItem()->child(j)->text(i).toStdString() << endl;
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    output of ->
    Top Level Item 0: April 04, 2013
    Child 0: 6:12:23 PM
    Child 0: 6:12:27 PM
    Child 0: 6:12:29 PM
    Child 0: 6:12:31 PM
    Top Level Item 1: Candy, Candy, Candy, Candy, Candy, Candy, Candy, Candy, Candy, Candy
    Child 1: Candy
    Child 1: Candy, Candy
    Child 1: Candy, Candy, Candy
    Child 1: Candy, Candy, Candy, Candy
    Top Level Item 2: 12.7
    Child 2: 1.27
    Child 2: 2.54
    Child 2: 3.81
    Child 2: 5.08
    Top Level Item 3: 125
    Child 3: 12.5
    Child 3: 25
    Child 3: 37.5
    Child 3: 50
    Top Level Item 4: 80
    Child 4: 8
    Child 4: 16
    Child 4: 24
    Child 4: 32
    Top Level Item 5: 90.4
    Child 5: 9.04
    Child 5: 18.08
    Child 5: 27.12
    Child 5: 36.16
    Top Level Item 6: 210
    Child 6: 21
    Child 6: 42
    Child 6: 63
    Child 6: 84
    Top Level Item 7: 30
    Child 7: 3
    Child 7: 6
    Child 7: 9
    Child 7: 12
    Top Level Item 8: 810
    Child 8: 81
    Child 8: 162
    Child 8: 243
    Child 8: 324
    ill now just use this data and add the correct amount of text edits and then set them to the correct values.

Similar Threads

  1. How to change QStackedWidget index from Qdialog
    By sliverTwist in forum Qt Programming
    Replies: 5
    Last Post: 20th March 2013, 20:24
  2. Change QVBoxLayout index?
    By davidlamhauge in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2012, 01:51
  3. Change QTableView's index number displayed
    By rex in forum Qt Programming
    Replies: 0
    Last Post: 6th April 2011, 16:30
  4. Replies: 4
    Last Post: 2nd March 2011, 10:03
  5. Replies: 8
    Last Post: 15th May 2007, 10: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.