Nominate our 2022 Qt Champions!

[SOLVED]QProcess run external cmd.exe with interactive input

  • Hi,
    I want to run and external *.exe program , that can ask for input like (y/n)....
    How can i run this program from qt and be able to enter the input ?
    i know about readstandarouput() and displaying it , but i need to know how to Input stuff not read .

        QStringList input_list = QStringList() << QString("-be") << QString("-pw") << QString(session->get_ftp_password())
                                << QString(QString(session->get_ftp_username())+"@"+QString(session->get_ftp_ip()))
                                << QString("-b") << QString("");
        process->start( "tools/psftp.exe",input_list,QIODevice::ReadOnly | QIODevice::Text);
            delete process;
            return false;
        delete process;
    here all i can do is run the process and get its output , i have no way to insert input .

  • Sorry to say, but I don't think you can. The process isn't connected to anything that you can write to.

    The only way I know to make this work is to provide all input via command line arguments and switches, which means the program itself must support that functionality.

    Assuming this is the same psftp that you are using, it looks like providing the --batch command may tell it to not ask you anything.

    If that's not the same psftp (or a different version, etc.), the same advice applies. Look up the program, determine what parameters you can use to disable interactivity and provide all necessary info on the command line.

    Hope that helps!

  • Moderators

    QProcess inherits QIODevice. You can use any of its write methods to write to the input stream of the process.

  • it is the same psftp , but the problem with --batch is when connecting to a server for the first time you need to accept the servers new key , and with --batch it doesn't ask you , by default it terminates the connection.

  • @Chris-Kawa thanks , i just wrote process->write("y\n") ;
    i hope there will be a better "solution" , because right now i don't know what the process asks and just give it y to continue .

  • Moderators

    Lets hope it doesn't ask you to format the hard drive ;)

    No, you should definitely not answer "y" without knowing the question. Connect to readyReadStandardOutput signal of the process. In the halndler read standard output, parse it and decide what to answer if needed.

  • @Chris-Kawa yeah thanks a lot thats what i did after your answer :D .

Log in to reply