QTProcess gets stuck wile running grep



  • 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@



  • 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.



  • 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;
    }
    @



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



  • 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.



  • 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;
    }
    @



  • [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.



  • 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.


  • 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


Log in to reply
 

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