Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt pushButton trigger terminal command



  • Hi. I am totally new to Qt.
    Is there any way that a Qt pushButton can trigger a terminal command? f.ex netcat?



  • @frodi
    What is a "terminal command"? I see a Linux netcat program. Does it keep running after you start it? Does it write stdout?

    I assume you mean you want to use QProcess to run a command. The detail of having that invoked when a QPushButton is clicked is neither here nor there. You have to decide where you want its output to show, e.g. in an xterm or read in & displayed by your calling program, etc.



  • Well, I am thinking of a scenario where i click a button an then a netcat command is executed in the linux terminal



  • @frodi
    In what Linux terminal?

    Yours is a GUI application which has button, right? So there is no terminal. You have to create one to run the command in, which is why I mentioned xterm. If that is what you need.

    I answered a question like this months ago. Have a look at https://forum.qt.io/topic/93735/start-terminal-with-command-by-qprocess/17, where I tracked down xterm -e <program> <argument> <argument> as the QProcess command-line. Is that what you want?


  • Lifetime Qt Champion

    @frodi You don't need a terminal to run executables (unless you really want to see a terminal for some reason).
    Simply use QProcess to execute the executables.



  • @jsulm I dont need to see the terminal. I just need to send a command via netcat. As I am totally new to this topic, could you give me an example of how i can use the command:

    echo '3b00010000001b010001000000120000013000002713000300030101' | xxd -r -p | nc 172.16.4.44 30013
    

    With the use of a QProcess?


  • Lifetime Qt Champion

    @frodi http://doc.qt.io/qt-5/qprocess.html

    QString program = "sh";
    QStringList arguments;
    arguments << "-c" << "echo" << "'3b00010000001b010001000000120000013000002713000300030101'" << "|" << "xxd" << "-r" << "-p" << "|" << "nc" << "172.16.4.44 30013";
    
    QProcess myProcess;
    myProcess.start(program, arguments);
    


  • @jsulm
    Although I admit I have not tried it, this does not look right. The syntax of /bin/sh (or /bin/bash) for a command is:

    /bin/sh -c "single-argument-to--c-argument"
    

    You are passing each bit as a separate argument to bin/sh -c, which will surely go wrong at least depending on what is in the arguments (whitespace, quotes, pipe symbols, ...)...?

    I would have expected something more like:

    QString program = "/bin/sh";
    QStringList arguments;
    arguments << "-c" << "echo '3b00010000001b010001000000120000013000002713000300030101' | xxd -r -p | nc 172.16.4.44 30013"
    

Log in to reply