QProcess::issues



  • HI, i keep geeting this error: QProcess:Destroyed, while process("cmd" )is still running. Here is my codes:

    QProcess procA();
       /procA.setWorkingDirectory("C:\\Users\\USER\\Desktop\\FileA\\FileB");
        procA.start("cmd",QStringList());
        procA.start("cmd");
        if(!procA.waitForStarted()){
            return ;
        }
        procA.write("mkdir newFolder");
        QMessageBox::information(this,tr("Completed"),tr("completed"));
        procA.closeWriteChannel();
        QByteArray outputA;
        if(procA.waitForReadyRead()){
            outputA += procA.readAll();
        }
        qDebug() << outputA;//your code here
    

    I dont understand why keep getting that error message . I dont really understand what is going on.


  • Moderators

    I guess this code is located in a function/method, right?
    The problem is: the "cmd" command is still running after creating the directory and as soon as the function/method is finished procA goes out of scope and is destroyed. That's why you get this message. You should first exit cmd and then destroy procA.

    Why do you use QProcess and cmd to create directories? You can use http://doc.qt.io/qt-5.6/qdir.html#mkdir



  • @jsulm

    Hi,

    Thanks for the feedback!Yes,Its located in a function. Regarding to "mkdir newFolder" , this is just a string that i wish to parse into cmd. The string could be anything . The idea is parse some string in to cmd, and the let the cmd do the rest of work.

    You also said "You should first exit cmd and then destroy procA" . Can you provide me an example for this . Thanks


  • Moderators

    Just pass "exit" command to the process like you did for "mkdir ..." and then call http://doc.qt.io/qt-5/qprocess.html#waitForFinished



  • @jsulm

         QProcess procA();
       procA.setWorkingDirectory("C:\\Users\\USER\\Desktop\\FileA\\FileB");
        procA.start("cmd",QStringList());
        
        if(!procA.waitForStarted()){
            return ;
        }
       ** procA.write("exit");
    procA.waitForFinished();**
        QMessageBox::information(this,tr("Completed"),tr("completed"));
        procA.closeWriteChannel();
        QByteArray outputA;
        if(procA.waitForReadyRead()){
            outputA += procA.readAll();
        }
    

    Something like this?


  • Moderators

    More like:

    QProcess procA();
    /procA.setWorkingDirectory("C:\\Users\\USER\\Desktop\\FileA\\FileB");
    procA.start("cmd",QStringList());
    procA.start("cmd");
    if(!procA.waitForStarted()){
        return ;
    }
    procA.write("mkdir newFolder");
    QMessageBox::information(this,tr("Completed"),tr("completed"));
    procA.closeWriteChannel();
    QByteArray outputA;
    if(procA.waitForReadyRead()){
        outputA += procA.readAll();
    }
    qDebug() << outputA;//your code here
    procA.write("exit");
    procA.waitForFinished();
    


  • This post is deleted!


  • @jsulm

    Hi, thank you for the example that you had provided. I did not get any error messages when i compile them. However, the QProcess doesnt execute properly, which means I did not see any newFolder in the directory. Not only that, every time i pressed the "exit"button on the ui form, whole GUI will stay in freeze mode.

    So, i assumed maybe that directory is not correct, the computer can not recognize the directory. Hence on, I modified my code into like this :

    procA.setWorkingDirectory("C://Users//USER//Desktop//FileA//FileB");
    procA.setWorkingDirectory("C:/Users/USER/Desktop/FileA/FileB/");
    procA.setWorkingDirectory("C:\Users\USER\Desktop\FileA\FileB");
    procA.setWorkingDirectory("C:\Users\USER\Desktop\FileA\File\B");

    Despite the modfication that i done for my code, the process still doesn't execute properly, i do not see any newFolder in that directory . What should I do to elimate / avoid this error keep happening . Thanks

    ***Ps: I m using window 8.1 64 bit


  • Moderators

    First: why do you have two start() calls?

    procA.start("cmd",QStringList());
    procA.start("cmd");
    

    Second: you have to append new line to each command you're sending to cmd (as you would do in cmd window to enter a command):

    procA.write("mkdir newFolder\n");
    

    For paths you can use slash:

    procA.setWorkingDirectory("C:/Users/USER/Desktop/FileA/FileB");
    

    With these changes it is working for me.

    The UI is freezing because you are waiting for QProcess in your UI thread.



  • Hi,

    speaking of this procA.start("cmd",QStringList());
    //procA.start("cmd");

    i was just trying which line will work.When i copy and paste the code to here, I was totally forget to remove one of the line. My apologies .

    Right now, the process is executing properly . Thanks to you!


Log in to reply
 

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