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. Problem building QStringlist
QtWS25 Last Chance

Problem building QStringlist

Scheduled Pinned Locked Moved Solved General and Desktop
qstringlist
7 Posts 3 Posters 2.1k 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
    jocala
    wrote on 28 Feb 2016, 19:39 last edited by
    #1

    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
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 28 Feb 2016, 19:50 last edited by
      #2

      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
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 28 Feb 2016, 20:41 last edited by
        #3

        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
        0
        • J Offline
          J Offline
          jocala
          wrote on 28 Feb 2016, 21:24 last edited by
          #4

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

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jocala
            wrote on 28 Feb 2016, 21:29 last edited by
            #5

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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 28 Feb 2016, 22:05 last edited by
              #6

              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
              1
              • J Offline
                J Offline
                jocala
                wrote on 28 Feb 2016, 23:16 last edited by
                #7

                I've got some reading to do :)

                Thanks @SGaist

                1 Reply Last reply
                0

                7/7

                28 Feb 2016, 23:16

                • Login

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