Using QProcess from a QTcpServer based class [SOLVED]



  • Hi,
    I am trying to use a QProcess object from a QTcpServer based class, but when I try to detect the moment a process is done, it seems the signal finished() never is called. Here is a little piece of the code I am using:

    void InternetService::directoryContent(const QString &path)
    {
        process = new QProcess(this);
        process->start("/bin/ls", QStringList() << path);
        connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), 
        this, SLOT(printDirectoryContent(int, QProcess::ExitStatus)));
    }
    
    void InternetService::printDirectoryContent(int exitCode, QProcess::ExitStatus exitStatus)
    {
        qDebug() << "Tracing this slot...";
    }
    

    Any suggestion about how to make it work? Thanks!



  • Hi,

    if there's some problem string the process QProcess emits the error() signal; you can connect a slot to it to understand what happens


  • Lifetime Qt Champion

    Hi,

    To add to @mcosta, you should connect everything before calling start.

    Also, you have a "memory leak" here, since every time you call directoryContent, you create new QProcess



  • Thank you!
    Using the error() signal I could trace the problem.

    connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
    

    But, now I have a new question: How can I capture the output of the process in an accurate way? I was trying to use the readLine() method but all I get is a NULL pointer exception:

            char *data = NULL;
            int size = process->readLine(data, 4);
    

    Any suggestion? Any small example to understand how to do it correctly? Thanks.



  • Hi,

    if you need to read the standard output after the process finished, the simplest way is to use QProcess: readAllStandardOutput()



  • It worked perfect! Thanks! :)



  • Even when the issue seems [SOLVED], if you're using 'ls' as the actual process to run (and not just as a simple example of how to use the QProcess() class) I would like to suggest you please consider using QDir and QFileInfo to deal with directories and files in a portable and platform independant way. See example [1] which may benefit your application in the future.

    [1] http://doc.qt.io/qt-5.4/qdir.html#details


Log in to reply
 

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