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. Write to MacOs Terminal with QProcess
QtWS25 Last Chance

Write to MacOs Terminal with QProcess

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.9k Views
  • 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.
  • B Offline
    B Offline
    Barcley
    wrote on last edited by
    #1

    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;
    }
    
    
    
    mrjjM 1 Reply Last reply
    0
    • B Barcley

      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;
      }
      
      
      
      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

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

        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.

        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
        0
        • B Offline
          B Offline
          Barcley
          wrote on last edited by
          #4

          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?

          mrjjM 1 Reply Last reply
          0
          • B Barcley

            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?

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

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

            B 1 Reply Last reply
            1
            • mrjjM mrjj

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

              B Offline
              B Offline
              Barcley
              wrote on last edited by
              #6

              @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 :)

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

                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.

                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

                • Login

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