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. Is it possible to send answer to cmd by QT?
Forum Updated to NodeBB v4.3 + New Features

Is it possible to send answer to cmd by QT?

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 5 Posters 2.7k Views 3 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.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by
    #1

    Many Times I used cmd I have to answer the question ( sometimes yes / no, sometimes other words ). How to send some words by QProcess?

    jsulmJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Your description is a bit vague.
      You can add parameters to the call of the other .exe but
      unless the .exe directly supports it, you cannot set predefined answers to any questions it might ask.

      1 Reply Last reply
      1
      • T TomNow99

        Many Times I used cmd I have to answer the question ( sometimes yes / no, sometimes other words ). How to send some words by QProcess?

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @TomNow99 said in Is it possible to send answer to cmd by QT?:

        How to send some words by QProcess?

        https://doc.qt.io/qt-5/qiodevice.html#write-2

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        mrjjM 1 Reply Last reply
        5
        • jsulmJ jsulm

          @TomNow99 said in Is it possible to send answer to cmd by QT?:

          How to send some words by QProcess?

          https://doc.qt.io/qt-5/qiodevice.html#write-2

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @jsulm
          so if you call write in the right order, it will be used correctly for the interactive process?

          jsulmJ 1 Reply Last reply
          1
          • mrjjM mrjj

            @jsulm
            so if you call write in the right order, it will be used correctly for the interactive process?

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @mrjj I would first wait for readyRead, check whether I get the input mask (like "Press Y/N" or what ever the console app writes) and then write. Then the order should be correct.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            mrjjM 1 Reply Last reply
            5
            • jsulmJ jsulm

              @mrjj I would first wait for readyRead, check whether I get the input mask (like "Press Y/N" or what ever the console app writes) and then write. Then the order should be correct.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @jsulm
              ah. so its possible to make small state machines and use write to answer.
              cool. I didn't know write could be used that way.

              1 Reply Last reply
              1
              • T Offline
                T Offline
                TomNow99
                wrote on last edited by TomNow99
                #7

                @jsulm @mrjj
                I have 2 apps:
                The first of them:

                #include <QCoreApplication>
                #include "iostream"
                
                using namespace std;
                
                int main(int argc, char *argv[])
                {
                    QCoreApplication a(argc, argv);
                
                    string answer;
                
                    cout<<"Do you prefer forest or sea?";
                
                    cin>>answer;
                    if(answer == "forest")
                    {
                        cout<<"You prefer forest!";
                    }
                    else if(answer == "sea")
                    {
                        cout<<"You prefer sea!";
                    }
                    else
                    {
                        cout<<"This answer is bad!";
                    }
                    exit(0);
                    return a.exec();
                }
                

                The second one:

                #include "mainwindow.h"
                #include "ui_mainwindow.h"
                #include <QDebug>
                MainWindow::MainWindow(QWidget *parent)
                    : QMainWindow(parent)
                    , ui(new Ui::MainWindow)
                {
                    ui->setupUi(this);
                    QProcess proc;
                    proc.start(R"(C:\Users\tom\Desktop\app\consoleApp.exe)");
                    proc.waitForStarted();
                    
                    while(proc.waitForReadyRead())
                        qDebug()<<proc.readAll();
                    proc.write("sea");
                }
                
                MainWindow::~MainWindow()
                {
                    delete ui;
                }
                

                I tried using "write", but with no positive result.

                I also tried:

                #include "mainwindow.h"
                #include "ui_mainwindow.h"
                #include <QDebug>
                MainWindow::MainWindow(QWidget *parent)
                    : QMainWindow(parent)
                    , ui(new Ui::MainWindow)
                {
                    ui->setupUi(this);
                    QProcess proc;
                    proc.start(R"(C:\Users\tom\Desktop\app\consoleApp.exe)");
                
                    proc.waitForReadyRead();
                    qDebug()<<proc.readAllStandardOutput();
                    proc.write("sea");
                }
                
                MainWindow::~MainWindow()
                {
                    delete ui;
                }
                
                

                But I get "QProcess: Destroyed while process ("C:\Users\tom\Desktop\app\consoleApp.exe") is still running."

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  proc is local to the constructor, it's destroyed right after your call to write and your other app is still running hence the error message.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • T Offline
                    T Offline
                    TomNow99
                    wrote on last edited by
                    #9

                    @SGaist Exactly.

                    I change it and now my code looks like:

                    #include "mainwindow.h"
                    #include "ui_mainwindow.h"
                    #include <QDebug>
                    MainWindow::MainWindow(QWidget *parent)
                        : QMainWindow(parent)
                        , ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                        
                        proc = new QProcess;
                        connect(proc, SIGNAL(readyRead()), this, SLOT(text()));
                        proc->start(R"(C:\Users\tom\Desktop\app\consoleApp.exe)");
                        proc->waitForStarted();
                        proc->waitForReadyRead();
                    
                        proc->write("sea");
                    
                    }
                    
                    MainWindow::~MainWindow()
                    {
                        delete ui;
                    }
                    
                    void MainWindow::text()
                    {
                        qDebug()<<proc->readAllStandardOutput();
                    }
                    

                    I still don't get the information that I prefer sea.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      You are mixing the blocking and non blocking API which is a bad idea.

                      Do either but not both.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • T Offline
                        T Offline
                        TomNow99
                        wrote on last edited by TomNow99
                        #11

                        @SGaist I change code :)

                        #include "mainwindow.h"
                        #include "ui_mainwindow.h"
                        #include <QDebug>
                        MainWindow::MainWindow(QWidget *parent)
                            : QMainWindow(parent)
                            , ui(new Ui::MainWindow)
                        {
                            ui->setupUi(this);
                        
                            proc = new QProcess;
                            proc->start(R"(C:\Users\tom\Desktop\app\consoleApp.exe)");
                            proc->waitForStarted();
                            proc->waitForReadyRead();
                            qDebug()<<proc->readAllStandardOutput();
                            proc->write("sea");
                            
                            qDebug()<<"I see you";
                            proc->waitForReadyRead();
                            qDebug()<<proc->readAllStandardOutput();
                            qDebug()<<"I don't see you";
                        }
                        
                        MainWindow::~MainWindow()
                        {
                            delete ui;
                        }
                        
                        

                        But still don't see that answer. I don't see "I don't see you" text too.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Maybe because the signal is not emitted. Since the application ends after writing to cout, you can use waitForFinished and then read its output.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          2
                          • T Offline
                            T Offline
                            TomNow99
                            wrote on last edited by
                            #13

                            I give up... Please for snippet :)

                            JonBJ 1 Reply Last reply
                            0
                            • T TomNow99

                              I give up... Please for snippet :)

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #14

                              @TomNow99
                              I think: assuming you do see "I see you" but you do not see "I don't see you", then replace the second occurrence of

                              proc->waitForReadyRead();
                              

                              by

                              proc->waitForfinished();
                              
                              1 Reply Last reply
                              1
                              • T Offline
                                T Offline
                                TomNow99
                                wrote on last edited by
                                #15

                                @JonB

                                #include "mainwindow.h"
                                #include "ui_mainwindow.h"
                                #include <QDebug>
                                MainWindow::MainWindow(QWidget *parent)
                                    : QMainWindow(parent)
                                    , ui(new Ui::MainWindow)
                                {
                                    ui->setupUi(this);
                                
                                        proc = new QProcess;
                                        proc->start(R"(C:\Users\tom\Desktop\app\consoleApp.exe)");
                                        proc->waitForStarted();
                                        proc->waitForReadyRead();
                                        qDebug()<<proc->readAllStandardOutput();
                                        proc->write("sea");
                                
                                        qDebug()<<"I see you";
                                        proc->waitForFinished();
                                        qDebug()<<proc->readAllStandardOutput();
                                        qDebug()<<"I don't see you";
                                }
                                
                                MainWindow::~MainWindow()
                                {
                                    delete ui;
                                }
                                

                                I change the second "waitForReadyRead". But still I don't see " I don't see" ( I see but after 30 seconds - I think the 30 seconds is a time when proc is terminate ).

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Then you should add some error checking in your code.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  2
                                  • T Offline
                                    T Offline
                                    TomNow99
                                    wrote on last edited by
                                    #17

                                    @jsulm Could you look at my code and told me what I have to change?

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • T TomNow99

                                      @jsulm Could you look at my code and told me what I have to change?

                                      jsulmJ Online
                                      jsulmJ Online
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @TomNow99 Besides what @SGaist suggested, what does first "qDebug()<<proc->readAllStandardOutput();" output?

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      2
                                      • T Offline
                                        T Offline
                                        TomNow99
                                        wrote on last edited by TomNow99
                                        #19

                                        @SGaist @jsulm @JonB @mrjj

                                        I add to my second App slots and a few changes like this ( I don't change consoleApp ):

                                        #include "mainwindow.h"
                                        #include "ui_mainwindow.h"
                                        #include <QDebug>
                                        MainWindow::MainWindow(QWidget *parent)
                                            : QMainWindow(parent)
                                            , ui(new Ui::MainWindow)
                                        {
                                            ui->setupUi(this);
                                            proc = new QProcess;
                                            connect(proc, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(stateChangedSlot(QProcess::ProcessState)));
                                            connect(proc, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(finishedSlot(int , QProcess::ExitStatus )));
                                            connect(proc, SIGNAL(errorOccurred(QProcess::ProcessError )), this, SLOT(errorOccurredSlot(QProcess::ProcessError )));
                                        
                                            proc->start(R"(C:\Users\tom\Desktop\app\consoleApp.exe)");
                                            proc->waitForStarted();
                                            proc->waitForReadyRead();
                                            qDebug()<<proc->readAllStandardOutput();
                                            proc->write("sea");
                                            qDebug()<<"I see you";
                                            proc->waitForReadyRead();
                                            qDebug()<<proc->readAllStandardOutput();
                                            qDebug()<<"I don't see you";
                                        }
                                        
                                        MainWindow::~MainWindow()
                                        {
                                            delete ui;
                                        }
                                        
                                        void MainWindow::stateChangedSlot(QProcess::ProcessState state)
                                        {
                                            qDebug()<<"From stateChangedSlot: "<<state;
                                        }
                                        
                                        void MainWindow::finishedSlot(int exitCode, QProcess::ExitStatus exitStatus)
                                        {
                                            qDebug()<<"From finishedSlot: "<<exitCode<<exitStatus;
                                        }
                                        
                                        void MainWindow::errorOccurredSlot(QProcess::ProcessError error)
                                        {
                                            qDebug()<<"From errorOccuredSlot: "<<error;
                                        }
                                        
                                        

                                        The output from qDebug() is:

                                        From stateChangedSlot:  QProcess::Starting
                                        From stateChangedSlot:  QProcess::Running
                                        "Do you prefer forest or sea?"
                                        I see you
                                        ""
                                        I don't see you
                                        

                                        First I get:

                                        From stateChangedSlot:  QProcess::Starting
                                        From stateChangedSlot:  QProcess::Running
                                        "Do you prefer forest or sea?"
                                        I see you
                                        

                                        Next I wait 30 seconds ( this is default value for waitForReadyRead ) and I get:

                                        ""
                                        I don't see you
                                        

                                        And qMainWindow change state to visible.

                                        Then I close my Application because nothing to do.

                                        JonBJ 1 Reply Last reply
                                        0
                                        • T TomNow99

                                          @SGaist @jsulm @JonB @mrjj

                                          I add to my second App slots and a few changes like this ( I don't change consoleApp ):

                                          #include "mainwindow.h"
                                          #include "ui_mainwindow.h"
                                          #include <QDebug>
                                          MainWindow::MainWindow(QWidget *parent)
                                              : QMainWindow(parent)
                                              , ui(new Ui::MainWindow)
                                          {
                                              ui->setupUi(this);
                                              proc = new QProcess;
                                              connect(proc, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(stateChangedSlot(QProcess::ProcessState)));
                                              connect(proc, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(finishedSlot(int , QProcess::ExitStatus )));
                                              connect(proc, SIGNAL(errorOccurred(QProcess::ProcessError )), this, SLOT(errorOccurredSlot(QProcess::ProcessError )));
                                          
                                              proc->start(R"(C:\Users\tom\Desktop\app\consoleApp.exe)");
                                              proc->waitForStarted();
                                              proc->waitForReadyRead();
                                              qDebug()<<proc->readAllStandardOutput();
                                              proc->write("sea");
                                              qDebug()<<"I see you";
                                              proc->waitForReadyRead();
                                              qDebug()<<proc->readAllStandardOutput();
                                              qDebug()<<"I don't see you";
                                          }
                                          
                                          MainWindow::~MainWindow()
                                          {
                                              delete ui;
                                          }
                                          
                                          void MainWindow::stateChangedSlot(QProcess::ProcessState state)
                                          {
                                              qDebug()<<"From stateChangedSlot: "<<state;
                                          }
                                          
                                          void MainWindow::finishedSlot(int exitCode, QProcess::ExitStatus exitStatus)
                                          {
                                              qDebug()<<"From finishedSlot: "<<exitCode<<exitStatus;
                                          }
                                          
                                          void MainWindow::errorOccurredSlot(QProcess::ProcessError error)
                                          {
                                              qDebug()<<"From errorOccuredSlot: "<<error;
                                          }
                                          
                                          

                                          The output from qDebug() is:

                                          From stateChangedSlot:  QProcess::Starting
                                          From stateChangedSlot:  QProcess::Running
                                          "Do you prefer forest or sea?"
                                          I see you
                                          ""
                                          I don't see you
                                          

                                          First I get:

                                          From stateChangedSlot:  QProcess::Starting
                                          From stateChangedSlot:  QProcess::Running
                                          "Do you prefer forest or sea?"
                                          I see you
                                          

                                          Next I wait 30 seconds ( this is default value for waitForReadyRead ) and I get:

                                          ""
                                          I don't see you
                                          

                                          And qMainWindow change state to visible.

                                          Then I close my Application because nothing to do.

                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by JonB
                                          #20

                                          @TomNow99
                                          For my part at least, I struggle to understand what the issue is, because we don't see what consoleApp.exe does/is supposed to do/how it is written, and you don't actually tells us what the expected behaviour is versus what happens in your output shown.

                                          So what is the actual problem? Please state clearly & concisely.

                                          Are you saying consoleApp.exe is supposed to see the proc->write("sea"); and respond to it? Or quit? Or what? How do we know what it expects, e.g. do you need to send a \n on the end of that string??

                                          EDIT Ah, I see now. I think you said at the start it has code cin>>answer;. You would have to look it up, but doesn't that have to wait for either a \n terminator or end-of-file before that returns? Is that all your problem is? When you test that app from the console, don't you have to type the Enter key after the word before it responds? Also, I am unclear how the QProcess channel works text-wise on Windows, you might want to append \r\n instead of just \n to your string.

                                          I'm not a great fan of the QProcess::waitFor...() blocking calls. You might be better using signals/slots for the whole thing, like you have done for the three slots you have so far.

                                          1 Reply Last reply
                                          3

                                          • Login

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