Bad index return from function...
-
Hi, i write a little code for search doubles in a tree list of directories/files at drop time (just for forbid doubles entries)...
but there is something i'm not able to understand, when i return the matched index, if i try to show the content data (who is at the index row at column 4 of my treeview), the data is wrong and not correspond to the right index.This is just a prototype (memeber function "search") that i have to use also at each construct row (childs of directory drop) time for check also each content of a dropped directory (because the content of the directory could be an other directory/file who is allready inside the Tree... and then, i need to delete the double allready present...). I'm not sure to be clear in my formulation... but the finality is to forbid totaly double in the tree.
here is the part of my code:
https://gist.github.com/anonymous/a811d0518fe85ac0613d
(more description of what's happen on the pasted file)could someone understand and tell me what i do wrong please ?
-
i post a part of the code here also... (hope the window of this web site is not to much stretch...)
@
QModelIndex Files_Model::search(QString text
int column,
QModelIndex parent) {
QMessageBox msgBox;
QModelIndex target_index, target_parent;
QString target_data;
for(int i; i < this->rowCount(parent); i++) {
target_index = this->index(i ,column , parent);
target_parent = this->index(i, 0, parent);
target_data = target_index.data().toString();
if(target_data == text) {
msgBox.setText(QString("M A T C H\n"
"target:\n"
"%1\n"
"data:\n"
"%2")
.arg(text)
.arg(target_data)); // OK, perfect
msgBox.exec();
return target_index; } // normaly the index targeted by search
if(this->hasChildren(parent))
search(text, column, target_parent); }
}QString Files_Model::Check_doubles_for(
const QFileInfo &file_info) {
QString file_find;
QMessageBox msgbox;
QString search_file = file_info.absoluteFilePath();
if (this->hasChildren()) {
QModelIndex idx_find = search(search_file,
4,
this->index(-1,0));
if(idx_find.isValid()) {
file_find = idx_find.data().toString();
msgbox.setText(QString("This file exist allready:\n%1")
.arg(file_find)); // not OK... bad data: WHY ?
msgbox.exec(); } }
return file_find; // so... bad data returned
}
@ -
Ok, this is resolved.
My error was to never break the loop after find a result... so the recursive call of self function continue and give back a wrong index.The solution was to:
break at find time,
and add a else if instead a if condition for childrens search recursivity to stop multiple and unused recursive calls
and add a condition at end time for return the target only one time
i think the code could be better... if i find better, i will post it.