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. Running GREP from QProcess (pyqt4)
Qt 6.11 is out! See what's new in the release blog

Running GREP from QProcess (pyqt4)

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 8.1k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello,

    I just can't get this to run correctly. I am trying to run the grep via QProcess, as follows:

    self.process = QtCore.QProcess()
    self.arguments_test = QtCore.QStringList()

    self.arguments_test << "-rn" << "hello" << "/home/user/*";
    self.process.start("grep", self.arguments_test)
    self.process.waitForFinished()

    I get : <PyQt4.QtCore.QStringList object at 0xb4776df4>

    I've tried running simpler commands such as ls, with parameters too and it works.

    Thanks in advance.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      QProcess won't do shell wildcard expansion for you. Invoke a shell (sh -c or such) to perform that.

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Hello pepe,

        Thanks for replying. So, do you have any basic code sample for invoking the shell? What is the class I should be looking at?

        Thanks.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on last edited by
          #4

          QProcess is the right class :) you just need to invoke a shell to perform that wildcard expansion.
          Something like
          @
          QStringList options;
          options << "-c" << "grep -rn hello /tmp/*";
          QProcess process;
          process.start("/bin/sh", options);
          @

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            I will try that! thanks a lot for your help.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Brijesh Yadav
              wrote on last edited by
              #6

              Hi dangelog your code works perfectly.A ton of thaks to you.But i am searching for a way to provide the search pattern name in a variable so that the user can give any pattern to search

              QString pattern = "hello";
              options << "-c" << "grep -r -w -i -o -c "<<pattern<<" hello /tmp/* ";

              doesn't work...

              1 Reply Last reply
              0

              • Login

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