Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Bad index return from function...

Bad index return from function...

Scheduled Pinned Locked Moved General and Desktop
3 Posts 1 Posters 675 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jerome_isAviable
    wrote on 7 Jan 2015, 06:08 last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jerome_isAviable
      wrote on 7 Jan 2015, 06:19 last edited by
      #2

      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
      }
      @

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jerome_isAviable
        wrote on 7 Jan 2015, 10:41 last edited by
        #3

        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.

        1 Reply Last reply
        0

        1/3

        7 Jan 2015, 06:08

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved