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;
    }
    
    
    

  • Qt Champions 2016

    @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 Test

    Does 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.


  • Lifetime Qt Champion

    Hi,

    Terminal.app is just a GUI that gives you a shell. If you want to call functions from the shell then you should do as @mrjj is suggesting and start the interpreter from your system.



  • 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?


  • Qt Champions 2016

    @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.



  • @mrjj said in Write to MacOs Terminal with QProcess:

    Try to use complete path to the avr-gcc.exe in your .sh file.

    YES!
    Now it works. Thank you a lot :)


  • Lifetime Qt Champion

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.