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. Problems with providing arguments to QProcess
QtWS25 Last Chance

Problems with providing arguments to QProcess

Scheduled Pinned Locked Moved Solved General and Desktop
desktopqprocessqt4qstringlist
10 Posts 8 Posters 7.1k 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.
  • T Offline
    T Offline
    Thanos
    wrote on last edited by
    #1

    I am trying to execute a c program from a QT application. I use the following code
    for the same but it does not work.

    QProcess *process = new QProcess();
    QString program = "sh";
    QString str1 = "gcc";
    QString str2 = "/home/Desktop/QtTest/basic.c";
    QString res = str1+ " " + str2;
    QStringList arguments;
    arguments << "-c" << res;
    process->start(program,arguments);
    
    if (!process->waitForStarted())
    {
    	qDebug("error in start");	
        return false;
    }
    process->closeWriteChannel();
    if (!process->waitForFinished())
    {
    	qDebug("error in finnished");	
        return false;
    } 
    

    But if I use --help as argument instead of filepath the command is executed successfully.

    QProcess *process = new QProcess();
    QString program = "sh";
    QString str1 = "gcc";
    QString str2 = "--help";
    QString res = str1+ " " + str2;
    QStringList arguments;
    arguments << "-c" << res;
    process->start(program,arguments);
    
    if (!process->waitForStarted())
    {
    	qDebug("error in start");	
        return false;
    }
    process->closeWriteChannel();
    if (!process->waitForFinished())
    {
    	qDebug("error in finnished");	
        return false;
    } 
    

    I am not able to understand why such behaviour and what is wrong on my side.
    As I know, I am missing something here.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      First - why do you execute sh instead gcc directly?
      Second - the arguments should be passed as separate arguments instead manually concatenated - otherwise they are not recognized as two separate arguments.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • T Offline
        T Offline
        Thanos
        wrote on last edited by
        #3

        I tried executing gcc directly, but I am facing the same problem as asked previously.

           QProcess process = new QProcess(); 
           QString program = "gcc";
           QStringList arguments;
           arguments << "--help";// "/home/sanket/Desktop/QtTest/basic.c";
           process->start(program,arguments);
        

        the gcc --help command is executed, but gcc along with the commented line as argument returns an empty string for readAllStandardOutput and readAllStandardError functions.

        1. How can I pass separate arguments to start function as it takes two arguments one as QString and other as QStringList
        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          First of all. if gcc can just compile the file. nothing is shown. no output at all.
          Fooled me at first.

          This code works for me and show any syntax errors if i introduce one.

          #include <QMessageBox>
          #include <QProcess>
          int main(int argc, char* argv[]) {
            QApplication a(argc, argv);
            QProcess process;
            QString program = "gcc";
            QStringList arguments;
            process.setProcessChannelMode(QProcess::MergedChannels);
            arguments << "-Wall" << "/home/master/test.cpp" << "-o" << "hello";
            process.start(program, arguments);
            process.waitForFinished(); // sets current thread to sleep and waits for process end ( lacks check)
            QString output(process.readAllStandardOutput());
            QMessageBox::warning(0, "output", output );
          
            MainWindow w;
            w.show();
          
            return a.exec();
          }
          
          

          test.cpp is

          #include <stdio.h>
          ddd
          int
          main (void)
          {
            printf ("Hello, world!\n");
            return 0;
          }
          

          and this is what sample says:
          alt text

          1 Reply Last reply
          4
          • T Offline
            T Offline
            Thanos
            wrote on last edited by
            #5

            Hi,

            Sorry for not mentioning it correctly.
            I was trying to say, that there is no output file (.out file ) generated.
            But now everything seems to work fine.

            Thank you for the hep

            aha_1980A 1 Reply Last reply
            1
            • T Thanos

              Hi,

              Sorry for not mentioning it correctly.
              I was trying to say, that there is no output file (.out file ) generated.
              But now everything seems to work fine.

              Thank you for the hep

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Thanos

              thanks for your feedback.

              So please mark this topic as SOLVED now.

              Qt has to stay free or it will die.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                LusuQT
                wrote on last edited by
                #7

                Hi
                Is there a way to use the below command in qProcess
                systeminfo | findstr /C:"OS Name"
                I tried to use it in this way

                QProcess m_process;
                QString OSCommand = "systeminfo | findstr /C:'OS Name'";
                m_process.start(OSCommand);
                m_process.waitForFinished(); 
                

                This didn't work,
                How can we handle these double quotes in our QString?

                jsulmJ JonBJ 2 Replies Last reply
                0
                • L LusuQT

                  Hi
                  Is there a way to use the below command in qProcess
                  systeminfo | findstr /C:"OS Name"
                  I tried to use it in this way

                  QProcess m_process;
                  QString OSCommand = "systeminfo | findstr /C:'OS Name'";
                  m_process.start(OSCommand);
                  m_process.waitForFinished(); 
                  

                  This didn't work,
                  How can we handle these double quotes in our QString?

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

                  @LusuQT said in Problems with providing arguments to QProcess:

                  How can we handle these double quotes in our QString?

                  By escaping them:

                  QString OSCommand = "systeminfo | findstr /C:\"OS Name\"";
                  

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

                  1 Reply Last reply
                  0
                  • L LusuQT

                    Hi
                    Is there a way to use the below command in qProcess
                    systeminfo | findstr /C:"OS Name"
                    I tried to use it in this way

                    QProcess m_process;
                    QString OSCommand = "systeminfo | findstr /C:'OS Name'";
                    m_process.start(OSCommand);
                    m_process.waitForFinished(); 
                    

                    This didn't work,
                    How can we handle these double quotes in our QString?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @LusuQT
                    Be aware of a couple of things with that command.

                    The QProcess::start() overload which accepts just a string (program + arguments) as a parameter has been removed. Though a QProcess::startCommand(const QString &command, QIODeviceBase::OpenMode mode = ReadWrite) has been added at Qt 6.0.

                    QProcess::start(const QString &program, const QStringList &arguments = {}, QIODeviceBase::OpenMode mode = ReadWrite), or QProcess::execute(const QString &program, const QStringList &arguments = {}), takes arguments as a list.

                    Because your command uses a *shell redirection token", the | character, this command string must be passed to the shell (cmd) for interpretation, else it won't work.

                    Your command should be:

                    m_process.start("cmd", { "/c", OSCommand } );
                    

                    Since you are going to have read the output from this command (presumably, else there is no point in issuing it), in this case you might save yourself some time/hassle if all you do is run the simple

                    m_process.start("systeminfo");
                    

                    and do you own searching for string OS Name as you read. That would save the extra process, the | piping and the need to use cmd /c.

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

                      Hi,

                      There is a simpler method: setStandardOutputProcess.

                      Use one QProcess for each element of your pipe. That way you recreate your chain in code and no dependency on shell handling.

                      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

                      • Login

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