Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Send arguments to LinuX Terminal with QProcess
Forum Updated to NodeBB v4.3 + New Features

Send arguments to LinuX Terminal with QProcess

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 5 Posters 7.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Alexanov
    22 Aug 2017, 07:33

    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?

    M Offline
    M Offline
    m.sue
    wrote on 22 Aug 2017, 08:13 last edited by m.sue
    #3

    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.

    1 Reply Last reply
    1
    • J jsulm
      22 Aug 2017, 07:53

      @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).

      A Offline
      A Offline
      Alexanov
      wrote on 22 Aug 2017, 08:53 last edited by Alexanov
      #4

      @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();
      
      J 1 Reply Last reply 22 Aug 2017, 08:57
      0
      • A Alexanov
        22 Aug 2017, 08:53

        @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();
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 22 Aug 2017, 08:57 last edited by
        #5

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        A 1 Reply Last reply 22 Aug 2017, 10:27
        3
        • J jsulm
          22 Aug 2017, 08:57

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

          A Offline
          A Offline
          Alexanov
          wrote on 22 Aug 2017, 10:27 last edited by Alexanov
          #6

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

          J R 2 Replies Last reply 22 Aug 2017, 11:23
          0
          • A Alexanov
            22 Aug 2017, 10:27

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

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 22 Aug 2017, 11:23 last edited by
            #7

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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • A Alexanov
              22 Aug 2017, 10:27

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

              R Offline
              R Offline
              Rajesh Bhau
              wrote on 27 May 2018, 11:46 last edited by
              #8

              @Alexanov Hi Alexanov , are you able to sequentially send and read output ?? if yes, please attach a code snippet .
              i tied this ,using write command but after one write command i had to use "closewriteterminal()" command in order to read standardoutput or standarderror. And next time , i cant send the write command since write terminal is closed. So i have to again use the start command to start my process. following is code snippet:

              QProcess myprocess ;
              myprocess.start("python");
              myprocess.write("print("hello")");
              myprocess.waitForFinished();
              myprocess.closeWriteChannel();
              while(myprocess.waitForFinished())
              {
              qDebug()<<myprocess.readAll();
              }

              // again i have to start the process
              myprocess.start("python");

              //and then the next write command
              myprocess.write("print("world")");

              can anybody tell better method to do it using Qprocess or any other . All solutions are welcom :)

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 27 May 2018, 12:01 last edited by
                #9

                Hi and welcome to the forums.
                You start python in interactive mode so process never terminates.
                maybe you can make it quit with
                myprocess.write("exit()");

                R 1 Reply Last reply 27 May 2018, 12:10
                0
                • M mrjj
                  27 May 2018, 12:01

                  Hi and welcome to the forums.
                  You start python in interactive mode so process never terminates.
                  maybe you can make it quit with
                  myprocess.write("exit()");

                  R Offline
                  R Offline
                  Rajesh Bhau
                  wrote on 27 May 2018, 12:10 last edited by
                  #10

                  @mrjj Thanks for your suggestios. My worry is not about how to close python session. But how to send 2 write commands without starting again the python. Which means that one time only i will open the qPROCESS as python and then sequentially i write some command to python and read the output of python.

                  M 1 Reply Last reply 27 May 2018, 12:15
                  0
                  • R Rajesh Bhau
                    27 May 2018, 12:10

                    @mrjj Thanks for your suggestios. My worry is not about how to close python session. But how to send 2 write commands without starting again the python. Which means that one time only i will open the qPROCESS as python and then sequentially i write some command to python and read the output of python.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 27 May 2018, 12:15 last edited by
                    #11

                    @Rajesh-Bhau
                    Ok so you want it to be interactive ?
                    see
                    https://forum.qt.io/topic/90904/how-do-i-run-docker-using-qprocess/8
                    i start python there and its interactive.
                    But its only for windows.

                    R 1 Reply Last reply 27 May 2018, 12:29
                    0
                    • M mrjj
                      27 May 2018, 12:15

                      @Rajesh-Bhau
                      Ok so you want it to be interactive ?
                      see
                      https://forum.qt.io/topic/90904/how-do-i-run-docker-using-qprocess/8
                      i start python there and its interactive.
                      But its only for windows.

                      R Offline
                      R Offline
                      Rajesh Bhau
                      wrote on 27 May 2018, 12:29 last edited by
                      #12

                      @mrjj Thanks for the answer, i want to make Qtextedit to mimic like python console. So i am trying to open python as qprocess in background and then input from Qtextedit will got to Python.exe to process and based on that the standard output will get printed back to Qtextedit.
                      i tried using Qprocess its working fine except that i cant do sequential input\output as i told i have to close the write terminal and start it again.

                      M 2 Replies Last reply 27 May 2018, 12:35
                      0
                      • R Rajesh Bhau
                        27 May 2018, 12:29

                        @mrjj Thanks for the answer, i want to make Qtextedit to mimic like python console. So i am trying to open python as qprocess in background and then input from Qtextedit will got to Python.exe to process and based on that the standard output will get printed back to Qtextedit.
                        i tried using Qprocess its working fine except that i cant do sequential input\output as i told i have to close the write terminal and start it again.

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 27 May 2018, 12:35 last edited by mrjj
                        #13

                        @Rajesh-Bhau
                        Yeah and for that to work, at least on windows, the process must be interactive for that to work.
                        it will just run command and exit. ( i think)
                        Looking at your code it seems to do that. so after print, it does terminate ?

                        1 Reply Last reply
                        0
                        • R Rajesh Bhau
                          27 May 2018, 12:29

                          @mrjj Thanks for the answer, i want to make Qtextedit to mimic like python console. So i am trying to open python as qprocess in background and then input from Qtextedit will got to Python.exe to process and based on that the standard output will get printed back to Qtextedit.
                          i tried using Qprocess its working fine except that i cant do sequential input\output as i told i have to close the write terminal and start it again.

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 27 May 2018, 12:39 last edited by
                          #14

                          @Rajesh-Bhau
                          Oh maybe -i will help
                          python -i foo.py
                          do not quit
                          https://www.johnny-lin.com/cdat_tips/tips_pylang/keep_open.html
                          forget about the interactive shell. you dont want to type so that should not be needed.

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved