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. QTProcess gets stuck wile running grep
Qt 6.11 is out! See what's new in the release blog

QTProcess gets stuck wile running grep

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 4.7k 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.
  • S Offline
    S Offline
    sambatyon
    wrote on last edited by
    #1

    I have the following small C++ Program, which I am trying to run on windows 7, grep is part of mingw and the minwg powered version of Qt:

    @
    #include <QStringList>
    #include <QProcess>
    #include <iostream>

    const int64_t kBuffSize = 2048;

    int main(int argc, char *argv[]) {
    QProcess grep;
    QStringList params;
    params << "-e" << "".*pas'"" << ""C:\Path To File\file.dpr"";//argv[1];
    grep.start("C:\MinGW\msys\1.0\bin\grep.exe", params);
    grep.setReadChannel(QProcess::StandardOutput);
    if (!grep.waitForFinished(60000)) {
    // program always exits here
    if (grep.state() == QProcess::Running)
    grep.kill();
    return 1;
    }
    std::cout << "ready to read" << std::endl;
    char buffer[kBuffSize];
    while (grep.readLine(buffer, kBuffSize) > 0) {
    std::cout << buffer;
    }
    if (grep.state() == QProcess::Running)
    grep.kill();
    return 0;
    }
    @

    The problem is grep never returns anything and the output of the program is always @QProcess: Destroyed while process still running@

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      QProcess is an asynch beast, like any QIODevice. Almost any real Qt program needs at least a QCoreApplication and a running eventloop. You have neither.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sambatyon
        wrote on last edited by
        #3

        I also tried:

        @
        #include <QStringList>
        #include <QProcess>
        #include <QCoreApplication>
        #include <iostream>

        const int64_t kBuffSize = 2048;

        int main(int argc, char *argv[]) {
        QCoreApplication app(argc, argv);
        QProcess grep;
        QStringList params;
        params << "-e" << "".*pas'"" << ""C:\Path To File\file.dpr"";//argv[1];
        grep.start("C:\MinGW\msys\1.0\bin\grep.exe", params);
        grep.setReadChannel(QProcess::StandardOutput);
        if (!grep.waitForFinished(60000)) {
        // program always exits here
        if (grep.state() == QProcess::Running)
        grep.kill();
        return 1;
        }
        std::cout << "ready to read" << std::endl;
        char buffer[kBuffSize];
        while (grep.readLine(buffer, kBuffSize) > 0) {
        std::cout << buffer;
        }
        if (grep.state() == QProcess::Running)
        grep.kill();
        return 0;
        }
        @

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sambatyon
          wrote on last edited by
          #4

          As it seems, the problem is in grep. But I cannot figure out why

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            You create the application object, but you do not start the event loop... Note that I'm not a 100% sure that that is the problem.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              Don't double quote your arguments. With your args here:

              @
              params << "-e" << "".*pas'"" << ""C:\Path To File\file.dpr"";//argv[1];
              @

              you search for lines that start with a quote (") followed by some characters followed by literal 'pas' followed by apostrophe (') followed by quote ("). I doubt that this is what you want.

              Also, your file path is "C:\Path To File\file.dpr" (including the quotes), which does not exist, as no path can start with a quote.

              What you probably want is:

              @
              params << "-e" << ".*pas'" << "C:\Path To File\file.dpr";
              @

              Also, you can use forward slashes for the path to the exe, Qt handles this for you.

              To see what's going wrong, show the error string:

              @
              if (!grep.waitForFinished(60000)) {
              qDebug() << grep.errorString();
              // program always exits here
              if (grep.state() == QProcess::Running)
              grep.kill();
              return 1;
              }
              @

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                [quote author="Andre" date="1298645066"]You create the application object, but you do not start the event loop... Note that I'm not a 100% sure that that is the problem.[/quote]

                At least on Mac OS X (and the other Unices) it works without a Q(Core)Application object. QProcess should start it's own event loop.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  QProcess does not need an event loop unless you don't use queued signals :-):

                  "QProcess docs":http://doc.qt.nokia.com/latest/qprocess.html :

                  waitForStarted and waitForFinished:

                  bq. This function can operate without an event loop.

                  but some points:

                  • setReadChannel after start? What if the process finished before that is executed?
                  • readLine might not return anything if your process does not output new lines.

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sambatyon
                    wrote on last edited by
                    #9

                    ok guys, for the interested, here is the solution of the problem. It seems I had to call first wait for started i got the solution "Here":http://stackoverflow.com/questions/5116663/qprocess-is-stuck

                    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