Using QProcess whilst in IDE



  • I want to use QProcess to execute 'free' and capture the output so I can monitor memory usage whilst my application is running, I've tried:

    const QString strCommand { "free" };
    
    static QProcess p;
    
    if ( p.isOpen() != true ) {
        p.start(strCommand);
    
        if ( !p.waitForFinished( -1 ) ) {
            qWarning() << "Error:" << p.readAllStandardError();
            return;
        }
        const QString strOutput = p.readAllStandardOutput();
        QStringList slstOutput = strOutput.split("\n");
    
        qDebug() << QString("---------------------");
        qDebug() << slstOutput;
    }
    

    However whilst this works when the application is launched from a terminal it doesn't work from within the QtCreator IDE and causes the application to hang up, where I have to kill the thread to get control of the IDE back.


  • Moderators

    @Tim-Webster said in Using QProcess whilst in IDE:

    if ( p.isOpen() != true ) {

    why do you check isOpen()? It is not needed at all.
    Connect a slot to http://doc.qt.io/qt-5/qprocess.html#errorOccurred signal and check whether you get an error.
    Your app most probably blocks when calling waitForFinished:
    "If msecs is -1, this function will not time out." You should put a timeout value != -1 and test again.



  • Because I have a timer performing this function at regular intervals and I want to ensure that I am not starting another instance whilst one is already set-up.

    Also, why does it work outside of the IDE, but never in the IDE?


  • Moderators

    @Tim-Webster QtCreator prepares the environment before starting your app, this can have some effects. You really should set timeout to some value and check what happens (stderr and stdout output) and connect error signal to a slot.


Log in to reply
 

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