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. Run a Terminal Code with QProcess on Ubuntu 18
Forum Updated to NodeBB v4.3 + New Features

Run a Terminal Code with QProcess on Ubuntu 18

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 1.1k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    aliemrenebiler
    wrote on last edited by
    #1

    We try to run a command on terminal with QProcess. We don't have to see the terminal but the code should work exacly at the same time with clicking the button. But somehow the code doesnt work, we tried many ways to make it.

    Here is our mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QProcess>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_pushButton_clicked()
    {
        QProcess p;
        p.setProgram("screen firefox");
        // p.setWorkingDirectory("/..."); // if needed
        p.start();
        p.waitForFinished(-1);
    }
    
    JonBJ 1 Reply Last reply
    1
    • A Offline
      A Offline
      aliemrenebiler
      wrote on last edited by aliemrenebiler
      #11

      We found the solution. roslaunch is a ROS command but we didn't implemented ros.h. We had a ROS implemented example and tried that in it, it worked! All the code was fine but the problem was the library.

      Thank you all for your help.

      1 Reply Last reply
      0
      • A aliemrenebiler

        We try to run a command on terminal with QProcess. We don't have to see the terminal but the code should work exacly at the same time with clicking the button. But somehow the code doesnt work, we tried many ways to make it.

        Here is our mainwindow.cpp:

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        #include <QProcess>
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        
        void MainWindow::on_pushButton_clicked()
        {
            QProcess p;
            p.setProgram("screen firefox");
            // p.setWorkingDirectory("/..."); // if needed
            p.start();
            p.waitForFinished(-1);
        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @aliemrenebiler
        What is screen firefox supposed to do, how do we know? You need to put in error checking: slot onto all the signals for errors/finished, and grab any output on stdout/stderr. Then with that information you can figure out what is/is not going on, and ask for help if necessary.

        A 1 Reply Last reply
        2
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #3

          Hi

          Just a note first. p.waitForFinished(-1); will hang your app until "screen" exits which it
          normally wont.

          Anyway, if I run it like this

          void MainWindow::RunQProcess( ) {
          
            QProcess* process = new QProcess();
          
            QObject::connect(process, &QProcess::started, []() {
              qDebug() << "started";
            });
          
            QObject::connect(process, &QProcess::readyRead, [process]() {
              QTextStream outputStream(process->readAllStandardOutput());
              auto data = outputStream.readAll();
              qDebug() << "readyRead:" << data;
            });
          
            QObject::connect(process, &QProcess::readyReadStandardOutput, [process]() {
              QTextStream outputStream(process->readAllStandardOutput());
              auto data = outputStream.readAll();
              qDebug() << "readyReadStandardOutput" << " read:" << data;
            });
          
            QObject::connect(process, &QProcess::readyReadStandardError, [process]() {
              QTextStream errorStream(process->readAllStandardError());
              auto data = errorStream.readAll();
              qDebug() << "readyReadStandardError" << " read:" << data;
            });
          
            QObject::connect(process, qOverload<int, QProcess::ExitStatus>(&QProcess::finished),
            [process](int exitCode, QProcess::ExitStatus exitStatus) {
              auto res = "exitcode:" + (QString::number(exitCode) + " exitStatus:" + QString::number(exitStatus));
              qDebug() << "Status:" << res;
              process->deleteLater(); // clean up
            });
          
            process->start("screen", QStringList() << "firefox" );
          
          }
          

          I get

          started
          readyRead: "Must be connected to a terminal.\r\n"
          readyReadStandardOutput  read: ""
          Status: "exitcode:1 exitStatus:0"
          

          so my guess is there is no active TTY in the QProcess inviroment.

          A 2 Replies Last reply
          2
          • JonBJ JonB

            @aliemrenebiler
            What is screen firefox supposed to do, how do we know? You need to put in error checking: slot onto all the signals for errors/finished, and grab any output on stdout/stderr. Then with that information you can figure out what is/is not going on, and ask for help if necessary.

            A Offline
            A Offline
            aliemrenebiler
            wrote on last edited by
            #4

            @JonB It suppose to open the Firefox App. And it does when I write it in terminal. Thanks for your answer.

            1 Reply Last reply
            1
            • mrjjM mrjj

              Hi

              Just a note first. p.waitForFinished(-1); will hang your app until "screen" exits which it
              normally wont.

              Anyway, if I run it like this

              void MainWindow::RunQProcess( ) {
              
                QProcess* process = new QProcess();
              
                QObject::connect(process, &QProcess::started, []() {
                  qDebug() << "started";
                });
              
                QObject::connect(process, &QProcess::readyRead, [process]() {
                  QTextStream outputStream(process->readAllStandardOutput());
                  auto data = outputStream.readAll();
                  qDebug() << "readyRead:" << data;
                });
              
                QObject::connect(process, &QProcess::readyReadStandardOutput, [process]() {
                  QTextStream outputStream(process->readAllStandardOutput());
                  auto data = outputStream.readAll();
                  qDebug() << "readyReadStandardOutput" << " read:" << data;
                });
              
                QObject::connect(process, &QProcess::readyReadStandardError, [process]() {
                  QTextStream errorStream(process->readAllStandardError());
                  auto data = errorStream.readAll();
                  qDebug() << "readyReadStandardError" << " read:" << data;
                });
              
                QObject::connect(process, qOverload<int, QProcess::ExitStatus>(&QProcess::finished),
                [process](int exitCode, QProcess::ExitStatus exitStatus) {
                  auto res = "exitcode:" + (QString::number(exitCode) + " exitStatus:" + QString::number(exitStatus));
                  qDebug() << "Status:" << res;
                  process->deleteLater(); // clean up
                });
              
                process->start("screen", QStringList() << "firefox" );
              
              }
              

              I get

              started
              readyRead: "Must be connected to a terminal.\r\n"
              readyReadStandardOutput  read: ""
              Status: "exitcode:1 exitStatus:0"
              

              so my guess is there is no active TTY in the QProcess inviroment.

              A Offline
              A Offline
              aliemrenebiler
              wrote on last edited by
              #5

              @mrjj Thank you so much. I will try this and write what happened again. We found a way to use terminal commands with QProcess, I will send the final result later.

              1 Reply Last reply
              1
              • mrjjM mrjj

                Hi

                Just a note first. p.waitForFinished(-1); will hang your app until "screen" exits which it
                normally wont.

                Anyway, if I run it like this

                void MainWindow::RunQProcess( ) {
                
                  QProcess* process = new QProcess();
                
                  QObject::connect(process, &QProcess::started, []() {
                    qDebug() << "started";
                  });
                
                  QObject::connect(process, &QProcess::readyRead, [process]() {
                    QTextStream outputStream(process->readAllStandardOutput());
                    auto data = outputStream.readAll();
                    qDebug() << "readyRead:" << data;
                  });
                
                  QObject::connect(process, &QProcess::readyReadStandardOutput, [process]() {
                    QTextStream outputStream(process->readAllStandardOutput());
                    auto data = outputStream.readAll();
                    qDebug() << "readyReadStandardOutput" << " read:" << data;
                  });
                
                  QObject::connect(process, &QProcess::readyReadStandardError, [process]() {
                    QTextStream errorStream(process->readAllStandardError());
                    auto data = errorStream.readAll();
                    qDebug() << "readyReadStandardError" << " read:" << data;
                  });
                
                  QObject::connect(process, qOverload<int, QProcess::ExitStatus>(&QProcess::finished),
                  [process](int exitCode, QProcess::ExitStatus exitStatus) {
                    auto res = "exitcode:" + (QString::number(exitCode) + " exitStatus:" + QString::number(exitStatus));
                    qDebug() << "Status:" << res;
                    process->deleteLater(); // clean up
                  });
                
                  process->start("screen", QStringList() << "firefox" );
                
                }
                

                I get

                started
                readyRead: "Must be connected to a terminal.\r\n"
                readyReadStandardOutput  read: ""
                Status: "exitcode:1 exitStatus:0"
                

                so my guess is there is no active TTY in the QProcess inviroment.

                A Offline
                A Offline
                aliemrenebiler
                wrote on last edited by aliemrenebiler
                #6

                @mrjj We tried your way but there is a problem with qOverload. It is an int we assume but not indentified (use of undeclared identifier). Can you help again?

                I would like to explain what we are aiming to do:
                We have a folder called "display.launch", it is a Rviz folder that will open with "roslaunch display.launch" command. We basicly open the terminal and go to the direction of /home/said/qt_ws/src/sasi_solid_v12/launch/ and start the "roslaunch display.launch" command. It opens the Rviz model automaticly. We want to do it with a button click on Qt.

                So as summary:

                1. We should start a terminal (it can be hidden, no problem)
                2. Go to the desired direction.
                3. Start a command ("roslaunch display.launch" or something else...)
                1 Reply Last reply
                1
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  Hi
                  for which signal do you need qOverload ?
                  can you show the line that gives the error ?

                  1:Well that should be ok.

                  2: you can use
                  https://doc.qt.io/qt-5/qprocess.html#setWorkingDirectory

                  3: you might need to provide the full path to the roslaunch exe
                  for Qt to find it.

                  A 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    Hi
                    for which signal do you need qOverload ?
                    can you show the line that gives the error ?

                    1:Well that should be ok.

                    2: you can use
                    https://doc.qt.io/qt-5/qprocess.html#setWorkingDirectory

                    3: you might need to provide the full path to the roslaunch exe
                    for Qt to find it.

                    A Offline
                    A Offline
                    aliemrenebiler
                    wrote on last edited by
                    #8

                    @mrjj hi again.
                    qOverload is a part of the code that you sent us. We really don't know what it does. But it says unclerified so we couldn't make the code work.

                    ec8da3dc-add0-4e2b-9f9d-e95f35b34f20-image.png

                    We already used setWorkingDirectory and that works fine. Thank you.

                    The problem is we don't need the exe path because roslaunch is a command, not an exe. roslaunch automatically starts the Rviz exe and the folder that we wanna open in Rviz. So the need is not the path, a way to start the roslaunch command.

                    mrjjM jsulmJ 2 Replies Last reply
                    1
                    • A aliemrenebiler

                      @mrjj hi again.
                      qOverload is a part of the code that you sent us. We really don't know what it does. But it says unclerified so we couldn't make the code work.

                      ec8da3dc-add0-4e2b-9f9d-e95f35b34f20-image.png

                      We already used setWorkingDirectory and that works fine. Thank you.

                      The problem is we don't need the exe path because roslaunch is a command, not an exe. roslaunch automatically starts the Rviz exe and the folder that we wanna open in Rviz. So the need is not the path, a way to start the roslaunch command.

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

                      @aliemrenebiler
                      hi
                      ah sorry. the eye is the first place one goes blind... ;)

                      QObject::connect(process, qOverload<int, QProcess::ExitStatus>(&QProcess::finished),

                      https://doc.qt.io/qt-5/qtglobal.html#qOverload

                      I forgot in which version it came. Maybe your Qt is older.

                      anyway one can use a cast instead

                      QObject::connect(process, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
                        [process](int exitCode, QProcess::ExitStatus exitStatus) {
                          auto res = "exitcode:" + (QString::number(exitCode) + " exitStatus:" + QString::number(exitStatus));
                          qDebug() << "Status:" << res;
                          process->deleteLater(); // clean up
                        });
                      

                      qOverload is just syntax sugar to make it read nicer.

                      1 Reply Last reply
                      1
                      • A aliemrenebiler

                        @mrjj hi again.
                        qOverload is a part of the code that you sent us. We really don't know what it does. But it says unclerified so we couldn't make the code work.

                        ec8da3dc-add0-4e2b-9f9d-e95f35b34f20-image.png

                        We already used setWorkingDirectory and that works fine. Thank you.

                        The problem is we don't need the exe path because roslaunch is a command, not an exe. roslaunch automatically starts the Rviz exe and the folder that we wanna open in Rviz. So the need is not the path, a way to start the roslaunch command.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @aliemrenebiler said in Run a Terminal Code with QProcess on Ubuntu 18:

                        We really don't know what it does

                        Then please read documentation (https://doc.qt.io/qt-5/qtglobal.html#qOverload) and include QtGlobal as the documentation shows:

                        #include <QtGlobal>
                        

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1
                        • A Offline
                          A Offline
                          aliemrenebiler
                          wrote on last edited by aliemrenebiler
                          #11

                          We found the solution. roslaunch is a ROS command but we didn't implemented ros.h. We had a ROS implemented example and tried that in it, it worked! All the code was fine but the problem was the library.

                          Thank you all for your help.

                          1 Reply Last reply
                          0

                          • Login

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