Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Problem building QStringlist

    General and Desktop
    qstringlist
    3
    7
    1610
    Loading More Posts
    • 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
      jocala last edited by

      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,"..");
      
      
      }
      
      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

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

        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

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

          Something like:

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply Reply Quote 0
          • J
            jocala last edited by

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

            1 Reply Last reply Reply Quote 0
            • J
              jocala last edited by

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

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 1
                • J
                  jocala last edited by

                  I've got some reading to do :)

                  Thanks @SGaist

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post