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. start terminal with command by QProcess
Forum Updated to NodeBB v4.3 + New Features

start terminal with command by QProcess

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 4 Posters 19.7k 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.
  • S Offline
    S Offline
    saber
    wrote on 16 Aug 2018, 03:45 last edited by saber
    #1

    i wanted to start a specific terminal with a command by QProcess .
    i came up this code.

        QProcess *q1 = new QProcess;;
        QString k("htop");
        q1->setWorkingDirectory(QDir::homePath());
        q1->startDetached(k);
        q1->waitForFinished();
        qDebug()<< q1->readAllStandardOutput();
    

    another

     QProcess *q1 = new QProcess;;
        QString k("htop");
        QString l(QDir::homePath());
        QStringList args;
        args << k;
        q1->startDetached("xfce4-terminal",args,l);
        q1->waitForStarted();
        qDebug()<< args<< l;
    

    but nothing opening .

    J 1 Reply Last reply 16 Aug 2018, 07:51
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 16 Aug 2018, 05:28 last edited by
      #2

      Before trying with QProcess you type the command on the command prompt/terminal & check whether it starts the new terminal. After that you can try with QProcess.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      S 1 Reply Last reply 16 Aug 2018, 05:42
      1
      • D dheerendra
        16 Aug 2018, 05:28

        Before trying with QProcess you type the command on the command prompt/terminal & check whether it starts the new terminal. After that you can try with QProcess.

        S Offline
        S Offline
        saber
        wrote on 16 Aug 2018, 05:42 last edited by
        #3

        @dheerendra
        it works on the terminal. but it dose not open new terminal .it runs there.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 16 Aug 2018, 05:57 last edited by
          #4

          QProcess executes one single process. What you are trying to do is executing a shell command, not a process. The piping of commands is a feature of your shell.

          Put the command you want to be executed as an argument to sh after -c ("command"):

          QProcess q1;
          q1.start("sh", QStringList() << "-c" << "htop");
          

          And this varies between the OS's. On what OS are you, I asume linux, as thats what htop is usually run on.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          S 1 Reply Last reply 16 Aug 2018, 06:01
          1
          • J J.Hilk
            16 Aug 2018, 05:57

            QProcess executes one single process. What you are trying to do is executing a shell command, not a process. The piping of commands is a feature of your shell.

            Put the command you want to be executed as an argument to sh after -c ("command"):

            QProcess q1;
            q1.start("sh", QStringList() << "-c" << "htop");
            

            And this varies between the OS's. On what OS are you, I asume linux, as thats what htop is usually run on.

            S Offline
            S Offline
            saber
            wrote on 16 Aug 2018, 06:01 last edited by saber
            #5

            @J.Hilk said in start terminal with command by QProcess:

            QProcess q1;
            q1.start("sh", QStringList() << "-c" << "htop");

            i am in linux.
            and your way do nothing .
            i want a specific terminal app will run the command.

            NT: htop is a terminal app .

            1 Reply Last reply
            0
            • S saber
              16 Aug 2018, 03:45

              i wanted to start a specific terminal with a command by QProcess .
              i came up this code.

                  QProcess *q1 = new QProcess;;
                  QString k("htop");
                  q1->setWorkingDirectory(QDir::homePath());
                  q1->startDetached(k);
                  q1->waitForFinished();
                  qDebug()<< q1->readAllStandardOutput();
              

              another

               QProcess *q1 = new QProcess;;
                  QString k("htop");
                  QString l(QDir::homePath());
                  QStringList args;
                  args << k;
                  q1->startDetached("xfce4-terminal",args,l);
                  q1->waitForStarted();
                  qDebug()<< args<< l;
              

              but nothing opening .

              J Offline
              J Offline
              JonB
              wrote on 16 Aug 2018, 07:51 last edited by JonB
              #6

              @saber
              In your sample code you use startDetached(), and then try to call all sorts of things on the QProcess, like readAllStandardOutput(). Please read http://doc.qt.io/qt-5/qprocess.html#startDetached to realise you cannot do these things on a detached process. You also cannot run interactive terminals and expect to be able to collect their output. You are also making no effort to check return results from calls or see if there is any error output message from the OS....

              Begin by using start() to run the xterm terminal command. We know that works and opens a window; we don't know anything about the "terminals" you are trying to run. When you have got that working you can examine the situation with your desired terminal.

              S 1 Reply Last reply 16 Aug 2018, 07:59
              1
              • J JonB
                16 Aug 2018, 07:51

                @saber
                In your sample code you use startDetached(), and then try to call all sorts of things on the QProcess, like readAllStandardOutput(). Please read http://doc.qt.io/qt-5/qprocess.html#startDetached to realise you cannot do these things on a detached process. You also cannot run interactive terminals and expect to be able to collect their output. You are also making no effort to check return results from calls or see if there is any error output message from the OS....

                Begin by using start() to run the xterm terminal command. We know that works and opens a window; we don't know anything about the "terminals" you are trying to run. When you have got that working you can examine the situation with your desired terminal.

                S Offline
                S Offline
                saber
                wrote on 16 Aug 2018, 07:59 last edited by saber
                #7

                @JonB i just want to run a command on specific terminal.don't want ant output or result to work.
                there are some terminal apps in linux that i want to run from my app in separate window.that's it.

                in short . open a specific terminal in separate window and run a command.

                J 1 Reply Last reply 16 Aug 2018, 08:06
                0
                • S saber
                  16 Aug 2018, 07:59

                  @JonB i just want to run a command on specific terminal.don't want ant output or result to work.
                  there are some terminal apps in linux that i want to run from my app in separate window.that's it.

                  in short . open a specific terminal in separate window and run a command.

                  J Offline
                  J Offline
                  JonB
                  wrote on 16 Aug 2018, 08:06 last edited by JonB
                  #8

                  @saber said in start terminal with command by QProcess:

                  i just want to run a command on specific terminal.don't want ant output or result to work.

                  Your code has:

                  qDebug()<< q1->readAllStandardOutput();

                  It's your code, not mine!

                  i just want to run a command on specific terminal

                  Your code does not attempt to run any command in the terminal you specify. It just attempts to run the terminal, i.e. presumably interactively. And then you say "it doesn't work" when you're not trying to get it to do whatever it is that you actually want. If you want to run a command in the terminal, why don't you say so, and attempt to do so? We can help you, but not if we have to guess stuff....

                  S 1 Reply Last reply 16 Aug 2018, 08:10
                  0
                  • J JonB
                    16 Aug 2018, 08:06

                    @saber said in start terminal with command by QProcess:

                    i just want to run a command on specific terminal.don't want ant output or result to work.

                    Your code has:

                    qDebug()<< q1->readAllStandardOutput();

                    It's your code, not mine!

                    i just want to run a command on specific terminal

                    Your code does not attempt to run any command in the terminal you specify. It just attempts to run the terminal, i.e. presumably interactively. And then you say "it doesn't work" when you're not trying to get it to do whatever it is that you actually want. If you want to run a command in the terminal, why don't you say so, and attempt to do so? We can help you, but not if we have to guess stuff....

                    S Offline
                    S Offline
                    saber
                    wrote on 16 Aug 2018, 08:10 last edited by
                    #9

                    @JonB sorry . i am not an expert in c++ nor qt.i am doing the job that my co-devloper should do .he is off-line and i can't sit back.

                    those code are copy past from many question /answers.

                    qt-forum is the help i can get now .

                    if you have anything that will work , it will be helpful.

                    J 1 Reply Last reply 16 Aug 2018, 08:16
                    0
                    • S saber
                      16 Aug 2018, 08:10

                      @JonB sorry . i am not an expert in c++ nor qt.i am doing the job that my co-devloper should do .he is off-line and i can't sit back.

                      those code are copy past from many question /answers.

                      qt-forum is the help i can get now .

                      if you have anything that will work , it will be helpful.

                      J Offline
                      J Offline
                      JonB
                      wrote on 16 Aug 2018, 08:16 last edited by JonB
                      #10

                      @saber
                      This has nothing to do with C++. I am happy to help you but you will not say what it is you want to do. If you write earlier

                      i want a specific terminal app will run the command.

                      why won't you say what the command is you want to run?

                      • Do you want to run an interactive terminal, and the user sits inside it, typing stuff, till he exits?
                      • Do you want the terminal to start, run some other command, and then exit?
                      • Or what?

                      Did you try what I suggested with xterm to see that that does open a terminal window, which I think you're saying your attempts do not?

                      S 2 Replies Last reply 16 Aug 2018, 08:23
                      0
                      • J JonB
                        16 Aug 2018, 08:16

                        @saber
                        This has nothing to do with C++. I am happy to help you but you will not say what it is you want to do. If you write earlier

                        i want a specific terminal app will run the command.

                        why won't you say what the command is you want to run?

                        • Do you want to run an interactive terminal, and the user sits inside it, typing stuff, till he exits?
                        • Do you want the terminal to start, run some other command, and then exit?
                        • Or what?

                        Did you try what I suggested with xterm to see that that does open a terminal window, which I think you're saying your attempts do not?

                        S Offline
                        S Offline
                        saber
                        wrote on 16 Aug 2018, 08:23 last edited by
                        #11

                        @JonB thanks.
                        i want the first one.
                        and i am trying the xterm way.

                        1 Reply Last reply
                        0
                        • J JonB
                          16 Aug 2018, 08:16

                          @saber
                          This has nothing to do with C++. I am happy to help you but you will not say what it is you want to do. If you write earlier

                          i want a specific terminal app will run the command.

                          why won't you say what the command is you want to run?

                          • Do you want to run an interactive terminal, and the user sits inside it, typing stuff, till he exits?
                          • Do you want the terminal to start, run some other command, and then exit?
                          • Or what?

                          Did you try what I suggested with xterm to see that that does open a terminal window, which I think you're saying your attempts do not?

                          S Offline
                          S Offline
                          saber
                          wrote on 16 Aug 2018, 08:26 last edited by
                          #12

                          @JonB i tried this

                              QProcess *Process = new QProcess();
                                  QString exec = "xterm";
                                  QStringList params;
                                  params <<"ls";
                                  Process->start(exec,params);
                          
                          J 1 Reply Last reply 16 Aug 2018, 08:29
                          0
                          • S saber
                            16 Aug 2018, 08:26

                            @JonB i tried this

                                QProcess *Process = new QProcess();
                                    QString exec = "xterm";
                                    QStringList params;
                                    params <<"ls";
                                    Process->start(exec,params);
                            
                            J Offline
                            J Offline
                            JonB
                            wrote on 16 Aug 2018, 08:29 last edited by JonB
                            #13

                            @saber
                            That will not work.

                            Please, you are simply answering inconsistently to each question I ask. I asked whether you want to run an interactive terminal the user sits in and types or whether you wish it to execute a command, to which you replied "i want the first one". But the example you give you are trying (incorrectly) to pass ls to it. Which is the second one.

                            If & when you come back and clearly state what you actually wish to do I will answer. But while you say one thing and then try to do something quite different nobody can help you....

                            S 1 Reply Last reply 16 Aug 2018, 08:36
                            0
                            • J JonB
                              16 Aug 2018, 08:29

                              @saber
                              That will not work.

                              Please, you are simply answering inconsistently to each question I ask. I asked whether you want to run an interactive terminal the user sits in and types or whether you wish it to execute a command, to which you replied "i want the first one". But the example you give you are trying (incorrectly) to pass ls to it. Which is the second one.

                              If & when you come back and clearly state what you actually wish to do I will answer. But while you say one thing and then try to do something quite different nobody can help you....

                              S Offline
                              S Offline
                              saber
                              wrote on 16 Aug 2018, 08:36 last edited by
                              #14

                              @JonB ok.
                              a pushbutton will open the terminal with the htop commnad(as it is the terminal app).
                              then a separate window will open which contains the terminal with running the htop app.
                              if user input command(ctrl +c) or the window close button clicked the window will close.(that's why i mean user will interact with the terminal by keyboard).

                              in the command there will be other type of terminal app.

                              J 1 Reply Last reply 16 Aug 2018, 08:47
                              0
                              • S saber
                                16 Aug 2018, 08:36

                                @JonB ok.
                                a pushbutton will open the terminal with the htop commnad(as it is the terminal app).
                                then a separate window will open which contains the terminal with running the htop app.
                                if user input command(ctrl +c) or the window close button clicked the window will close.(that's why i mean user will interact with the terminal by keyboard).

                                in the command there will be other type of terminal app.

                                J Offline
                                J Offline
                                JonB
                                wrote on 16 Aug 2018, 08:47 last edited by JonB
                                #15

                                @saber
                                You still don't state what it is you want to happen in the terminal, I'm tearing my hair out! Does it sit there interactively doing nothing waiting for the user to type some commands, or does it run some command (which you won't tell us about), show its output, leave the window there awaitng you to only close it, and that's it?

                                Using xterm I'll show you both of these:

                                1. Sits interactively:
                                    QProcess *process = new QProcess();
                                    QString exec = "xterm";
                                    process->start(exec);
                                
                                1. Runs ls -l, output in the xterm, does not accept any further commands, leaves window open, user can close window:
                                    QProcess *process = new QProcess();
                                    QString exec = "xterm";
                                    QStringList params;
                                    params << "-hold" << "-e" << "ls" << "-l";
                                    process->start(exec, params);
                                

                                When one of these does whatever you want we might be getting somewhere....?

                                S 1 Reply Last reply 16 Aug 2018, 09:01
                                3
                                • J JonB
                                  16 Aug 2018, 08:47

                                  @saber
                                  You still don't state what it is you want to happen in the terminal, I'm tearing my hair out! Does it sit there interactively doing nothing waiting for the user to type some commands, or does it run some command (which you won't tell us about), show its output, leave the window there awaitng you to only close it, and that's it?

                                  Using xterm I'll show you both of these:

                                  1. Sits interactively:
                                      QProcess *process = new QProcess();
                                      QString exec = "xterm";
                                      process->start(exec);
                                  
                                  1. Runs ls -l, output in the xterm, does not accept any further commands, leaves window open, user can close window:
                                      QProcess *process = new QProcess();
                                      QString exec = "xterm";
                                      QStringList params;
                                      params << "-hold" << "-e" << "ls" << "-l";
                                      process->start(exec, params);
                                  

                                  When one of these does whatever you want we might be getting somewhere....?

                                  S Offline
                                  S Offline
                                  saber
                                  wrote on 16 Aug 2018, 09:01 last edited by saber
                                  #16

                                  @JonB i want the second one .
                                  just open the terminal with the commnad and sit there .
                                  here is the code for htop

                                  
                                  QString l(QDir::homePath());
                                      QProcess *process = new QProcess();
                                          QString exec = "xterm";
                                          QStringList params;
                                          params << "-hold" << "-e" << "htop" ;
                                          process->setWorkingDirectory(l);
                                          process->start(exec, params);
                                  

                                  output
                                  0_1534410012411_b1.png

                                  ther is now two things to do .

                                  1. extiting the command with the ctrl +c leave a empty window ,nothing in it.
                                    0_1534410000322_b2.png
                                  2. the command is running only in the xterm . i tried the xfce-terminal.i want all the known terminal should do this.

                                  thanks

                                  J 1 Reply Last reply 16 Aug 2018, 09:08
                                  0
                                  • S saber
                                    16 Aug 2018, 09:01

                                    @JonB i want the second one .
                                    just open the terminal with the commnad and sit there .
                                    here is the code for htop

                                    
                                    QString l(QDir::homePath());
                                        QProcess *process = new QProcess();
                                            QString exec = "xterm";
                                            QStringList params;
                                            params << "-hold" << "-e" << "htop" ;
                                            process->setWorkingDirectory(l);
                                            process->start(exec, params);
                                    

                                    output
                                    0_1534410012411_b1.png

                                    ther is now two things to do .

                                    1. extiting the command with the ctrl +c leave a empty window ,nothing in it.
                                      0_1534410000322_b2.png
                                    2. the command is running only in the xterm . i tried the xfce-terminal.i want all the known terminal should do this.

                                    thanks

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 16 Aug 2018, 09:08 last edited by JonB
                                    #17

                                    @saber
                                    If you do not want the xterm to remain open for closure, remove the -hold argument.

                                    If you want to use something other than xterm to run your htop (or whatever), the whole point is that I had to man xterm to discover that the way it runs a command for you is to specify -e <program> <argument> <argument>. So if you want xfce-terminal instead of xterm, you will need to read its manual page to discover what command-line it takes. There will not be a generic "i want all the known terminal should do this", it will vary from terminal to terminal.

                                    BTW, if I had wanted to ask your question, now that I know what it is, I would have said something like:

                                    "I want to run a program named htop from QProcess. However, the problem is that it needs a terminal window to display its output. How do I do that? And btw (for some reason) I'd like the terminal window to be provided by xfce-terminal."

                                    S 1 Reply Last reply 16 Aug 2018, 09:21
                                    2
                                    • J JonB
                                      16 Aug 2018, 09:08

                                      @saber
                                      If you do not want the xterm to remain open for closure, remove the -hold argument.

                                      If you want to use something other than xterm to run your htop (or whatever), the whole point is that I had to man xterm to discover that the way it runs a command for you is to specify -e <program> <argument> <argument>. So if you want xfce-terminal instead of xterm, you will need to read its manual page to discover what command-line it takes. There will not be a generic "i want all the known terminal should do this", it will vary from terminal to terminal.

                                      BTW, if I had wanted to ask your question, now that I know what it is, I would have said something like:

                                      "I want to run a program named htop from QProcess. However, the problem is that it needs a terminal window to display its output. How do I do that? And btw (for some reason) I'd like the terminal window to be provided by xfce-terminal."

                                      S Offline
                                      S Offline
                                      saber
                                      wrote on 16 Aug 2018, 09:21 last edited by saber
                                      #18

                                      @JonB in future i will take much time to clear the qustion.

                                      now i thought it will be easy to open htop in other terminal and that will be universal.

                                      because the htop app has a icon and when it clicked it opes the app in the defult terminal.

                                      J 1 Reply Last reply 16 Aug 2018, 09:33
                                      0
                                      • S saber
                                        16 Aug 2018, 09:21

                                        @JonB in future i will take much time to clear the qustion.

                                        now i thought it will be easy to open htop in other terminal and that will be universal.

                                        because the htop app has a icon and when it clicked it opes the app in the defult terminal.

                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 16 Aug 2018, 09:33 last edited by JonB
                                        #19

                                        @saber

                                        in future i will take much time to clear the qustion.

                                        Thank you :)

                                        because the htop app has a icon and when it clicked it opes the app in the defult terminal.

                                        You misunderstand how "launching an app from an icon" works. This does not only apply to htop.

                                        "Launching from an icon" under Linux is a feature of your Desktop Manager. And you could be using a variety of those under Linux, e.g. Unity, GNOME, I guess you're using XFCE. Almost certainly you have a htop.desktop file somewhere, probably hidden somewhere inside your user area (e.g. try running find ~ -name \*.desktop -print; or it might be in either of /usr/share/applications or ~/.local/share/applications). When the desktop launches from an icon it reads that file and that tells it what to do. And if you look in that file (it's text format) you''ll see something stating its a "console" or "terminal" application (e.g. Terminal=true; also see how there is a Exec=... to specify what exactly it is to execute). So when your desktop launches it the desktop creates the console/terminal window for it to run in automatically. When we do it from within another application, like a Qt one, we need to do that ourselves explicitly, hence what you've been trying.

                                        1 Reply Last reply
                                        4

                                        1/19

                                        16 Aug 2018, 03:45

                                        • Login

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