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.
  • dheerendraD dheerendra

    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 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.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on 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
      1
      • J.HilkJ J.Hilk

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

          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 .

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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
          1
          • JonBJ JonB

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

            JonBJ 1 Reply Last reply
            0
            • S saber

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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
              0
              • JonBJ JonB

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

                JonBJ 1 Reply Last reply
                0
                • S saber

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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
                  0
                  • JonBJ JonB

                    @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 last edited by
                    #11

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

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @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 last edited by
                      #12

                      @JonB i tried this

                          QProcess *Process = new QProcess();
                              QString exec = "xterm";
                              QStringList params;
                              params <<"ls";
                              Process->start(exec,params);
                      
                      JonBJ 1 Reply Last reply
                      0
                      • S saber

                        @JonB i tried this

                            QProcess *Process = new QProcess();
                                QString exec = "xterm";
                                QStringList params;
                                params <<"ls";
                                Process->start(exec,params);
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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
                        0
                        • JonBJ JonB

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

                          JonBJ 1 Reply Last reply
                          0
                          • S saber

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

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on 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
                            3
                            • JonBJ JonB

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

                              JonBJ 1 Reply Last reply
                              0
                              • S saber

                                @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

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on 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
                                2
                                • JonBJ JonB

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

                                  JonBJ 1 Reply Last reply
                                  0
                                  • S saber

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

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on 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

                                    • Login

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