Problem building QStringlist



  • I'm attempting to build a remote directory browser that gets its data via the *nix find command. I have it working for the most part, but I'm having trouble filtering out unwanted material. For example, any line returned that contains "find:" is an error of some type, usually permissions, and I want to toss it.

    In the code below, I loop through a stringlist of directories and files, tossing out blank lines with this statement:

    if(dirlist.at(i).isEmpty())
                  dirlist.removeOne(dirlist.at(i));
    

    This works as expected. However, when I attempt to toss out error lines:

      if(dirlist.at(i).contains("find:"))
             dirlist.removeOne(dirlist.at(i));
    

    The lines containing "find:" are not removed.

    The entire method follows below:

    void MainWindow::load_filemanager(QString dir)
    {
    
    ui->directoryWidget->clear();
    
     QString cstring=getadb()+" shell find "+dir+" -type d  maxdepth 1";
      
      QString command=RunProcess(cstring);
    
    cstring=getadb()+" shell find "+dir+" -type f -maxdepth 1";
    
     command=command+RunProcess(cstring);
    
        QStringList dirlist = command.split("\n");
    
         dirlist.removeOne(dirlist.at(0));
    
           for (int i = 0; i < dirlist.size(); ++i)
              {
    
               if(dirlist.at(i).isEmpty())
                   dirlist.removeOne(dirlist.at(i));
    
    
          if(dirlist.at(i).contains("find:"))
             dirlist.removeOne(dirlist.at(i));
    
    
           }
    
    
    ui->directoryWidget->addItems(dirlist);
    ui->directoryWidget->insertItem(0,"..");
    
    
    }
    

  • Qt Champions 2016

    Hi
    that is a bit odd.
    Maybe try with
    http://doc.qt.io/qt-5/qstring.html#indexOf
    with Qt::CaseInsensitive


  • Lifetime Qt Champion

    Hi,

    Why not filter what doesn't start with find: ?

    Something like:

    dirlist = dirlist.filter(QRegularExpression("^(?!find:)"));
    


  • Thanks for the replies. I went with the regex which seems to work quite well.



  • Could a regex filter out blank lines and eliminate the loop?


  • Lifetime Qt Champion

    Yes:
    dirlist = dirlist.filter(QRegularExpression("^(?!\s*$).+"));



  • I've got some reading to do :)

    Thanks @SGaist


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.