PDA

View Full Version : Edit Feature works perfect if I do not change index...



giblit
30th March 2013, 04:36
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:


void DateListDialog::updateTreeWidget(){
for (int i = 0; i < dates.size(); ++i) {

itemParent = new QTreeWidgetItem(treeWidget);
for(int j = 0; j<24; j++){
timeChild[j] = new QTreeWidgetItem(itemParent);
//timeChild[j]->setFlags(Qt::NoItemFlags);
}

itemParent->setText(1, foods[i]);
for(int k = 0; k<7; k++){
itemParent->setText(k+2, totalValues[k+(7*i)]);
//cout << totalValues[k+(7*i)].toStdString() << endl;
}

itemParent->setText(0, dates[i]);

for(int j = 0; j<24; j++){
timeChild[j]->setText(1, times[j+(24*i)]);
}

for(int i = 0; i<24; i++){
if(i == 0){
time = "12 AM";
} else if(i > 0 && i < 12){
time = QString("%1 AM").arg(i);
} else if(i == 12){
time = "12 PM";
} else if(i > 12){
time = QString("%1 PM").arg(i-12);
}
timeChild[i]->setText(0, time);

}

treeWidget->addTopLevelItem(itemParent);

}


allDates << dates;
allTimes << times;
/*for(int i = 0; i<allDates.size();i++){
for(int j = 0; j<24; j++){
if(!allTimes[j+(24*i)].contains("No Food")){
cout << allTimes[j+(24*i)].toStdString() << flush;
}
}
cout << endl;
}*/
allFoods << foods;
allValues << totalValues;

}
void DateListDialog::editDate()
{
dates.clear();
times.clear();
foods.clear();
totalValues.clear();
for(int i = 0; i<24;i++){
addedTimes[i].clear();
}
edit = true;
for(int i = 0; i<24; i++){
if(!allTimes[i].contains("No Food")){
addedTimes[i] = allTimes[i];
}
for(int i = 0; i<7; i++){
totalValue[i] = allValues[i].toInt();
}
}
posInt = treeWidget->indexOfTopLevelItem(treeWidget->currentItem());
ListWidgetDialog w;
w.exec();
if(changed == true){
for(int i = 23; i>=0; i--){
allTimes.removeAt(i+(24*posInt));
}
allDates.removeAt(posInt);
allFoods.removeAt(posInt);
for(int i = 6; i>=0; i--){
allValues.removeAt(i+(7*posInt));
}
// cout << "Changed = true" << endl;
treeWidget->takeTopLevelItem(posInt);
updateTreeWidget();
}
edit = false;
/* for(int i = 0; i<allTimes.size(); i++){
if(!allTimes[i].contains("No Food")){
cout << allTimes[i].toStdString() << endl;
}
}*/
}
void DateListDialog::treeWidget_clicked(){
//cout << "HELLO" << endl;
}
void DateListDialog::deleteDate()
{
posInt = treeWidget->indexOfTopLevelItem(treeWidget->currentItem());
if(posInt >= 0){
treeWidget->takeTopLevelItem(posInt);
for(int i = 23; i>=0; i--){
allTimes.removeAt(i+(24*posInt));
//cout << i+(24*posInt) << endl;
}
//cout << "SELECTED POSITION IS: " << posInt << endl;
allDates.removeAt(posInt);
allFoods.removeAt(posInt);
for(int i = 6; i>=0; i--){
allValues.removeAt(i+(7*posInt));
}

QFile file(QDir::homePath() + "/datedata.txt");
if (file.open(QIODevice::WriteOnly)) {
QTextStream out(&file);
out.setCodec("UTF-8");
for(int i = 0; i<allDates.size(); i++){
out << allDates[i] << endl;
for(int j = 0; j<24; j++){
out << allTimes[j+(24*i)] << endl;
}
out << allFoods[i] << endl;
for(int k = 0; k<7; k++){
allValues[k+(7*i)];
}

out << endl;
}
}
file.close();
}
//cout << "SIZE AFTER WRITE: " << allDates.size() << endl;


}

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
if(!allTimes[i].....);
//was suppsed to be
if(!allTimes[i+(24*posInt)]......)

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.

anda_skoa
2nd April 2013, 10:19
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,
_

giblit
5th April 2013, 06:03
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 =]

for(int i = 0; i<9; i++){
cout << "Top Level Item " << i << ": " << treeWidget->topLevelItem(treeWidget->indexOfTopLevelItem(treeWidget->currentItem()))->text(i).toStdString() << endl;
for(int j = 0; j<treeWidget->currentItem()->childCount(); j++){
cout << "Child " << i << ": " << treeWidget->currentItem()->child(j)->text(i).toStdString() << endl;
}
}
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.