Write to MacOs Terminal with QProcess
-
Hello Forum,
I try to write commands to my Terminal application and read the answers. Therefor I use the QProcess Class.
Unfortunately my code below does nothing...
What am I doing wrong?#include "mainwindow.h" #include "ui_mainwindow.h" #include <qprocess.h> #include <qdebug.h> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); /* create QProcess object */ proc1 = new QProcess(); proc1->start("/Applications/Utilities/Terminal.app",QStringList() << "echo Test"); /* show output */ connect(proc1, SIGNAL(readyReadStandardOutput()),this, SLOT(rightMessage1()) ); connect(proc1, SIGNAL(readyReadStandardError()), this, SLOT(errorMessage1()) ); /*-------------------------------------------------------------------------------*/ } MainWindow::~MainWindow() { delete ui; } void MainWindow::rightMessage1() { QByteArray data = proc1->readAllStandardOutput(); //arm1->setText(QString data); QString text = QString(data); qDebug() << text << endl; } void MainWindow::errorMessage1() { QByteArray data = proc1->readAllStandardOutput(); //arm1->setText(QString data); QString text = QString(data); qDebug() << text << endl; }
-
Hello Forum,
I try to write commands to my Terminal application and read the answers. Therefor I use the QProcess Class.
Unfortunately my code below does nothing...
What am I doing wrong?#include "mainwindow.h" #include "ui_mainwindow.h" #include <qprocess.h> #include <qdebug.h> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); /* create QProcess object */ proc1 = new QProcess(); proc1->start("/Applications/Utilities/Terminal.app",QStringList() << "echo Test"); /* show output */ connect(proc1, SIGNAL(readyReadStandardOutput()),this, SLOT(rightMessage1()) ); connect(proc1, SIGNAL(readyReadStandardError()), this, SLOT(errorMessage1()) ); /*-------------------------------------------------------------------------------*/ } MainWindow::~MainWindow() { delete ui; } void MainWindow::rightMessage1() { QByteArray data = proc1->readAllStandardOutput(); //arm1->setText(QString data); QString text = QString(data); qDebug() << text << endl; } void MainWindow::errorMessage1() { QByteArray data = proc1->readAllStandardOutput(); //arm1->setText(QString data); QString text = QString(data); qDebug() << text << endl; }
@Barcley
Hi
Normally you would give it a script to run
Prozess->start("/bin/sh", QStringList() << "Shell.sh");If you try it manually in a shell
/Applications/Utilities/Terminal.app echo TestDoes that work ?
I dont know macOs so I cant say. But it looks wrong.
But if echo is external command, it wont work.,
If echo is build in command, then it might work but..
On Windows, you must do
Process->start("C:/windows/system32/cmd.exe",QStringList()<<"/C"<<"C:/Users/admin/Desktop/test.txt");Note the /C. That is to make it see the script.
You might need something like on macOS or you dont - as in linux.
-
Ok Thank you for your replies.
Iam now able to run a shell script with an "echo" command in it:/* create QProcess object */ proc1 = new QProcess(); proc1->start("/bin/sh", QStringList() << "/Users/tobias/Desktop/Build.sh"); /* show output */ connect(proc1, SIGNAL(readyReadStandardOutput()),this, SLOT(rightMessage1()) ); connect(proc1, SIGNAL(readyReadStandardError()), this, SLOT(errorMessage1()) );
The script location is on my desktop:
#!/bin/bash echo asdasd cd /Users/tobias/Desktop/L_Meter avr-gcc -g -Os -mmcu=attiny2313 -c AtTiny.c avr-gcc -g -mmcu=attiny2313 -o main.elf AtTiny.o avr-objcopy -j .text -j .data -O ihex main.elf main.hex
and the console output from the redyReadStandartOutput/Error is:
Starte /Users/tobias/build-QtAVR-Desktop_Qt_5_8_0_clang_64bit-Debug/QtAVR.app/Contents/MacOS/QtAVR... shell answer: "asdasd\n" shell error: "" /Users/tobias/build-QtAVR-Desktop_Qt_5_8_0_clang_64bit-Debug/QtAVR.app/Contents/MacOS/QtAVR beendet, Rückgabewert 0
So you now might see the problem I have :)
The script should compile some c-files (in the directory "L_Meter") with the avr-gcc compiler. If I run the script manually with a double-click from the desktop or in my Terminal.app (gui) it works as it should.
But if the script is startet from my Qt-Program its not working. No object or .elf-files are created. But the "echo" command still works.Do you have an idea what iam doing wrong?
-
Ok Thank you for your replies.
Iam now able to run a shell script with an "echo" command in it:/* create QProcess object */ proc1 = new QProcess(); proc1->start("/bin/sh", QStringList() << "/Users/tobias/Desktop/Build.sh"); /* show output */ connect(proc1, SIGNAL(readyReadStandardOutput()),this, SLOT(rightMessage1()) ); connect(proc1, SIGNAL(readyReadStandardError()), this, SLOT(errorMessage1()) );
The script location is on my desktop:
#!/bin/bash echo asdasd cd /Users/tobias/Desktop/L_Meter avr-gcc -g -Os -mmcu=attiny2313 -c AtTiny.c avr-gcc -g -mmcu=attiny2313 -o main.elf AtTiny.o avr-objcopy -j .text -j .data -O ihex main.elf main.hex
and the console output from the redyReadStandartOutput/Error is:
Starte /Users/tobias/build-QtAVR-Desktop_Qt_5_8_0_clang_64bit-Debug/QtAVR.app/Contents/MacOS/QtAVR... shell answer: "asdasd\n" shell error: "" /Users/tobias/build-QtAVR-Desktop_Qt_5_8_0_clang_64bit-Debug/QtAVR.app/Contents/MacOS/QtAVR beendet, Rückgabewert 0
So you now might see the problem I have :)
The script should compile some c-files (in the directory "L_Meter") with the avr-gcc compiler. If I run the script manually with a double-click from the desktop or in my Terminal.app (gui) it works as it should.
But if the script is startet from my Qt-Program its not working. No object or .elf-files are created. But the "echo" command still works.Do you have an idea what iam doing wrong?
@Barcley said in Write to MacOs Terminal with QProcess:
avr-gcc
Hi
Try to use complete path to the avr-gcc.exe in your .sh file.
Also might be issue with path to the files.I would guess on path issue.
-
@Barcley said in Write to MacOs Terminal with QProcess:
avr-gcc
Hi
Try to use complete path to the avr-gcc.exe in your .sh file.
Also might be issue with path to the files.I would guess on path issue.
-
In that case, you can also setup the QProcess environment so that the path to your cross-compiler can be configured and added to the PATH environment variable of the process.