Running a Unix command programmatically with QT



  • I am just learning unix and QT. I want to send a command out through qt to Unix. I've tried this code and I don't get the result i do when I do it in the terminal. What is going wrong here?

    Header:
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "QProcess"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::MainWindow *ui;
    QProcess *m_process;
    };

    #endif // MAINWINDOW_H@

    CPP
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "QDebug"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    m_process = new QProcess(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    QString cmd = "printf "testnc" | nc localhost 1235";
    qDebug() << cmd;

    m_process->start(cmd);
    qDebug() << "Got here.";
    

    }
    @



  • When I click the go button, the code executes but my "listener" at the other end isn't getting the message.

    If I run the command:

    printf "testnc" | nc localhost 1235

    from the terminal, my listener picks it up fine.


  • Moderators

    Piping does not work in QProcess.

    You can simulate it be redirecting output from one QProcess to the other. Or use my "QEasyShell":https://github.com/sierdzio/qeasyshell project, it might just work ;P.


Log in to reply
 

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