I looked at various options for working with the console, but did not find the right one. It is necessary to bring the execution command to the console as if I entered it there myself. The fact is that most programs I can run with only one parameter using QProcess. Please show the working version of this solution.

process.start("mutt", QStringList << "param"); if( !process.waitForStarted() || !process.waitForFinished() ) { return; } 

More deployed:

 void transport_mail () { QString buf; QProcess process; buf = "mutt -s "; buf += '"'; buf += "tema1"; buf +='"'; buf += " mail@gmail.com"; buf += " < /home/alexandr/1.txt"; qDebug () << buf; process.start(buf); if( !process.waitForStarted() || !process.waitForFinished() ) { return; } qDebug() << process.readAllStandardError(); qDebug() << process.readAllStandardOutput(); } 

Displays in console:

 "mutt -s "tema1" mail@gmail.com < /home/alexandr/1.txt" QProcess: Destroyed while process ("mutt") is still running. 

But if there is one parameter, then everything is ok. If you type in the console yourself, then everything works too. UbuntuSDK - Qt5, C ++

  • that's right, I built it in memory now, the result is the same - shaman888
  • Write a simple program that will display its entire command line and try experimenting with it. Otherwise, it will be possible to guess for a long time what is being transmitted and how. - Vladimir Martyanov
  • The command is mutt -s "tema" mail@gmail.com </home/user/1.txt how to send it through QT to work? - shaman888

3 answers 3

QProcess really does not know how to work with commands that use I / O redirection. But it provides functions that allow them to emulate.

 QString command("mutt"); QStringList arguments; arguments << "-s" << "\"tema1\"" << "mail@gmail.com"; QProcess process; process.setStandardInputFile("/home/alexandr/1.txt"); // Изменить стандартный ввод process.start(command, arguments); 

Other useful methods:

 process.setStandardOutputFile("output.txt"); // Изменить стандартный вывод process.setStandardErrorFile("errors.txt"); // Изменить вывод ошибок // или даже так: QProcess process2; process1.setStandardOutputProcess(&process2); // аналог конвейера command1 | command2 process1.start(command1); process2.start(command2); 
  • Thanks for the example. - shaman888

QProcess for some reason does not want to work with input redirection >> <<> <when ​​they are passed as arguments to QStringList. Perhaps this problem can be solved by reading the documentation.

In the meantime, I offer this crutch with the creation of a temporary file on the disk.

 #include <QProcess> #include <QDebug> #include <QFile> int main(int argc, char *argv[]) { //создаём файл, вотором будет лежать bash-скрипт QFile tmp_file("run_command.sh"); tmp_file.open(QIODevice::WriteOnly | QIODevice::Text); //наполняем bash-скрипт содержимым tmp_file.write("#!/bin/bash\n"); tmp_file.write("mutt -s \"tema1\" mail@gmail.com < /home/alexandr/1.txt"); //строка "netstat -atunp4 > netstat.txt" работала, по идее и Ваша команда должна выполниться //устанавливаем права на чтение и исполнение tmp_file.setPermissions(QFile::ExeGroup | QFile::ExeOther | QFile::ExeOther | QFile::ExeUser | QFile::ReadOwner | QFile::ReadUser | QFile::ReadGroup | QFile::ReadOther); tmp_file.close(); //запускаем наш скрипт QProcess proc; proc.start("./run_command.sh"); proc.waitForFinished(); //удаляем временный файл QFile::remove("run_command.sh"); return 0; } 
  • A good example, did not check, but the crutch is universal. Thank. - shaman888
  • Checked Works. - shaman888

For example:

 QProcess pingProcess; QString exe = "ping ya.ru"; pingProcess.start(exe); pingProcess.waitForFinished(); qDebug() << pingProcess.readAllStandardOutput(); pingProcess.close(); 

Console:

 " Pinging ya.ru [213.180.193.3] with 32 bytes of data: Request timed out. Request timed out. Request timed out. Request timed out. Ping statistics for 213.180.193.3: Packets: Sent = 4, Received = 0, Lost = 4 (100% loss), " 

More options and more interesting:

 QProcess process; QString exe = "python -c \"for i in range(10): print('*' * i)\""; process.start(exe); process.waitForFinished(); qDebug() << process.readAllStandardOutput(); process.close(); 

Console:

 " * ** *** **** ***** ****** ******* ******** ********* " 
  • corrected the question. - shaman888
  • I have already shown the working options, it remains for you to fit in yourself. I don’t have access to your car, so I can’t test it with you. Try logging process.waitForStarted() and process.waitForFinished() , and to avoid the "QProcess: Destroyed while ..." error, call close() before exiting the function. I suspect that process.waitForStarted() returned false , which was inverted to true , and process.waitForFinished() not called because it would not affect the result of the condition - gil9red
  • I put my own line in the first version, the result has not changed much. Now it does not display an error message, but the command is still not executed. - shaman888
  • And just when running "mutt -s "tema1" mail@gmail.com < /home/alexandr/1.txt" in the terminal works? - gil9red
  • In the terminal works. process.waitForStarted () - true process.waitForFinished () - false - shaman888