Send arguments to LinuX Terminal with QProcess



  • Hi
    I want to send arguments to Linux terminal with Qprocess in Qt, like below:

    //my code
        QProcess *Process = new QProcess();
        QString exec = "konsole";
        QStringList params;
        params <<"ls";
        Process->start(exec,params,QIODevice::ReadWrite);
        Process->waitForFinished(-1);
        QString p_stdout = Process->readAllStandardOutput();
        qDebug()<<p_stdout;
    

    The konsole ran but QProcess did not send any arguments and finally I can not read any Output ! can some body help me, How can i run terminal of Linux in background , Send argument to that and receive the Output?


  • Moderators

    @Alexanov If you do not need any console window you can just use /bin/sh instead of konsole.
    For konsole you probably need to pass some parameter to tell it to execute the command you want (check its man page or other doc).



  • Hi @Alexanov

    You will have to connect to readyReadStandardOutput, readyReadStandardError. Start the linux command with process->start(...), collect the stdout- and stderr-output in the slots you connected with, and wait for the finished signal. I would put this all into a class of its own.

    -Michael.



  • @jsulm
    Hi thanks for your Reply , I knew that for run Konsole without Gui I have to use "sh" in proc.start(program,argument) command , program section.
    for send executable command to Konsole I knew that before send command I have to send "-c" like below , but how can i know that this properties (like -c for executable commadn)?I did not see any of argument properties in QProcess help link in Qt.

        QProcess proc;
        proc.start("sh", QStringList() << "-c" << "ls");
    
        proc.waitForFinished();
        QByteArray output = proc.readAll();
    

  • Moderators

    @Alexanov You will not find it in the QProcess documentation because it is completely unrelated to QProcess. Those are parameters for commands which you want to execute. So, if you want to execute sh you need to read sh documentation. If you want to execute konsole then read its documentation. QProcess just starts an executable in a process and passes the parameter you provide, it does not care what executable is started and which parameters it supports.



  • @jsulm Thanks , I will read konsole documentation ...
    My problem solved but i have little question that remained , if i want to send multiple executable commands and then see outputs ... how can i do that?(sequentially send and read output and go to next command )


  • Moderators

    @Alexanov You can write to the process using QProcess. There is an example here http://doc.qt.io/qt-5/qprocess.html


Log in to reply
 

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