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. Running a Unix command programmatically with QT

Running a Unix command programmatically with QT

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.2k Views
  • 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.
  • K Offline
    K Offline
    kloveridge
    wrote on 30 Mar 2014, 14:48 last edited by
    #1

    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.";
    

    }
    @

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kloveridge
      wrote on 30 Mar 2014, 14:50 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sierdzio
        Moderators
        wrote on 30 Mar 2014, 15:03 last edited by
        #3

        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.

        (Z(:^

        1 Reply Last reply
        0

        1/3

        30 Mar 2014, 14:48

        • Login

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