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. Cannot get the QProcess started
Forum Updated to NodeBB v4.3 + New Features

Cannot get the QProcess started

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 4 Posters 3.0k 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 Anonymous_Banned275

    Partially solved.
    The QPrtocess will not start if there are any spaces in the "command' or "arg".
    Still now writing to the file or console. .

    Addendum

    I need clarification on this
    I did venture to QProcess to have a better control then afforded by "system" calls.
    After all this I realized that I may have a better understanding of QProcess, however, I have a major misunderstanding of trying to replace
    system("hcotool dev >> /tmp/temp")

    with

    //Command = "hcitool";
    // args<<"dev"<<">>/tmp/temp";
    Oprocess.start(Command,args,QIODevice::ReadWrite);

    Basically
    how does “command , Arg “ get passed to the system similar to “system” call ?
    On top of that – I really do not understand what “device” is QIODevice
    accessing. My gut feeling is that this “OProcess.start” is incomplete to fully replace “system” call.

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

    @AnneRanch said in Cannot get the QProcess started:

    After all this I realized that I may have a better understanding of QProcess, however, I have a major misunderstanding of trying to replace
    system("hcotool dev >> /tmp/temp")

    I may not get much thanks for this, but to answer just this question. You cannot pass >> or >> /tmp/temp as an argument to QProcess. This is because only the Linux shell deals with redirection symbols such as >>. (This would also be the case if you use other redirection symbols such as >, < or |.)

    When you go system("hcitool dev >> /tmp/temp") what the library function system() does is issue this command: /bin/sh -c "hcitool dev >> /tmp/temp", which gets the /bin/sh shell to execute the command dealing with the >> redirection.

    To do the same from QProcess you need to go:

    OProcess.start("/bin/sh", QStringList() << "-c" << "hcitool dev >> /tmp/temp");
    // or shorter if you prefer, same thing:
    OProcess.start("/bin/sh", { "-c", "hcitool dev >> /tmp/temp" });
    

    That will work.

    A separate issue is that you would be better not doing it this way and instead handling the output redirection to a file or memory yourself, as @KroMignon shows one way. But if you want to do it the same way as your original system() call this is how to do it via QProcess.

    Christian EhrlicherC A 2 Replies Last reply
    4
    • JonBJ JonB

      @AnneRanch said in Cannot get the QProcess started:

      After all this I realized that I may have a better understanding of QProcess, however, I have a major misunderstanding of trying to replace
      system("hcotool dev >> /tmp/temp")

      I may not get much thanks for this, but to answer just this question. You cannot pass >> or >> /tmp/temp as an argument to QProcess. This is because only the Linux shell deals with redirection symbols such as >>. (This would also be the case if you use other redirection symbols such as >, < or |.)

      When you go system("hcitool dev >> /tmp/temp") what the library function system() does is issue this command: /bin/sh -c "hcitool dev >> /tmp/temp", which gets the /bin/sh shell to execute the command dealing with the >> redirection.

      To do the same from QProcess you need to go:

      OProcess.start("/bin/sh", QStringList() << "-c" << "hcitool dev >> /tmp/temp");
      // or shorter if you prefer, same thing:
      OProcess.start("/bin/sh", { "-c", "hcitool dev >> /tmp/temp" });
      

      That will work.

      A separate issue is that you would be better not doing it this way and instead handling the output redirection to a file or memory yourself, as @KroMignon shows one way. But if you want to do it the same way as your original system() call this is how to do it via QProcess.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #7

      @JonB https://forum.qt.io/topic/118925/qprocess-c-syntax-passing-program-and-arguments/9 :D

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

      JonBJ 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @JonB https://forum.qt.io/topic/118925/qprocess-c-syntax-passing-program-and-arguments/9 :D

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

        @Christian-Ehrlicher LOL.

        @AnneRanch In that link you asked that question before and I answered the same way. That remains valid.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Anonymous_Banned275
          wrote on last edited by
          #9

          OK, let me try this again.
          Here a full code

          void Form::on_pushButton_46_clicked()
          {
              qDebug(" START void Form::on_pushButton_46_clicked()");
              ReadTempFile();
              ClearTempFile();
              system("hciconfig -a >/tmp/temp"); // TOK builds file
              ReadTempFile();
              ClearTempFile();
              QProcess process;
              qDebug(" START  process.start..... -a  ");
              process.start("hciconfig", QStringList() << "-a"); // no output expected 
              process.waitForFinished();
              qDebug(" process.waitForFinished() hcicofig -a ");
              ReadTempFile();    // no data in tempo expected 
              process.start("hciconfig", QStringList() << "-a"<<" >>"<<"/tmp/temp");
          
          **THIS FAILS TO REDIRECT THE HCICONFIG TO TEMP FILE** 
            
              process.waitForFinished();
              qDebug(" process.waitForFinished() hcicofig -a >>/tmp/temp");
              qDebug(" process.waitForFinished()");
              ReadTempFile();
              qDebug(" NO DATA ReadTempFile()");
              process.start("hcitool", QStringList() << "dev"<<">>"<<"/tmp/temp");
              process.waitForFinished();
              process.start("hcitool", QStringList() << "dev"<<">>/tmp/temp");
              process.waitForFinished();
              qDebug(" End  void Form::on_pushButton_46_clicked()");
          }
          
          Here is the output. 
          
          
          

          10:46:25: Starting /home/qy/Qt/Examples/Qt-5.12.12/widgets/mainwindows/mdi/mdi...
          Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
          START void Form::on_pushButton_46_clicked()
          START Form::ReadTempFile()
          in.readLine().isEmpty()
          END Form::ReadTempFile()
          START void Form::ClearTempFile()
          in.readLine().isEmpty()
          END void Form::ClearTempFile()
          START Form::ReadTempFile()
          END Form::ReadTempFile()
          START void Form::ClearTempFile()
          in.readLine().isEmpty()
          END void Form::ClearTempFile()
          START process.start..... -a
          process.waitForFinished() hcicofig -a
          START Form::ReadTempFile()
          in.readLine().isEmpty()
          END Form::ReadTempFile()
          process.waitForFinished() hcicofig -a >>/tmp/temp
          process.waitForFinished()
          START Form::ReadTempFile()

          THetre ARE no DATA IN TEMP FILE

          in.readLine().isEmpty()
          END Form::ReadTempFile()
          NO DATA ReadTempFile()
          End void Form::on_pushButton_46_clicked()

          **The task of QProcess is to redirect output of command hciconfig to the temporary file.

          This line of code fails to do so:

          process.start("hciconfig", QStringList() << "-a"<<" >>"<<"/tmp/temp");**

          Can anybody help me to solve this?
          Do I have wrong syntax?

          KroMignonK 1 Reply Last reply
          0
          • A Anonymous_Banned275

            OK, let me try this again.
            Here a full code

            void Form::on_pushButton_46_clicked()
            {
                qDebug(" START void Form::on_pushButton_46_clicked()");
                ReadTempFile();
                ClearTempFile();
                system("hciconfig -a >/tmp/temp"); // TOK builds file
                ReadTempFile();
                ClearTempFile();
                QProcess process;
                qDebug(" START  process.start..... -a  ");
                process.start("hciconfig", QStringList() << "-a"); // no output expected 
                process.waitForFinished();
                qDebug(" process.waitForFinished() hcicofig -a ");
                ReadTempFile();    // no data in tempo expected 
                process.start("hciconfig", QStringList() << "-a"<<" >>"<<"/tmp/temp");
            
            **THIS FAILS TO REDIRECT THE HCICONFIG TO TEMP FILE** 
              
                process.waitForFinished();
                qDebug(" process.waitForFinished() hcicofig -a >>/tmp/temp");
                qDebug(" process.waitForFinished()");
                ReadTempFile();
                qDebug(" NO DATA ReadTempFile()");
                process.start("hcitool", QStringList() << "dev"<<">>"<<"/tmp/temp");
                process.waitForFinished();
                process.start("hcitool", QStringList() << "dev"<<">>/tmp/temp");
                process.waitForFinished();
                qDebug(" End  void Form::on_pushButton_46_clicked()");
            }
            
            Here is the output. 
            
            
            

            10:46:25: Starting /home/qy/Qt/Examples/Qt-5.12.12/widgets/mainwindows/mdi/mdi...
            Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
            START void Form::on_pushButton_46_clicked()
            START Form::ReadTempFile()
            in.readLine().isEmpty()
            END Form::ReadTempFile()
            START void Form::ClearTempFile()
            in.readLine().isEmpty()
            END void Form::ClearTempFile()
            START Form::ReadTempFile()
            END Form::ReadTempFile()
            START void Form::ClearTempFile()
            in.readLine().isEmpty()
            END void Form::ClearTempFile()
            START process.start..... -a
            process.waitForFinished() hcicofig -a
            START Form::ReadTempFile()
            in.readLine().isEmpty()
            END Form::ReadTempFile()
            process.waitForFinished() hcicofig -a >>/tmp/temp
            process.waitForFinished()
            START Form::ReadTempFile()

            THetre ARE no DATA IN TEMP FILE

            in.readLine().isEmpty()
            END Form::ReadTempFile()
            NO DATA ReadTempFile()
            End void Form::on_pushButton_46_clicked()

            **The task of QProcess is to redirect output of command hciconfig to the temporary file.

            This line of code fails to do so:

            process.start("hciconfig", QStringList() << "-a"<<" >>"<<"/tmp/temp");**

            Can anybody help me to solve this?
            Do I have wrong syntax?

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #10

            @AnneRanch said in Cannot get the QProcess started:

            Can anybody help me to solve this?
            Do I have wrong syntax?

            Do you read what other replies to your post?
            For example wat @JonB has written

            to do the same from QProcess you need to go:
            OProcess.start("/bin/sh", QStringList() << "-c" << "hcitool dev >> /tmp/temp");
            // or shorter if you prefer, same thing:
            OProcess.start("/bin/sh", { "-c", "hcitool dev >> /tmp/temp" });

            So this would be: process.start(/bin/sh", QStringList() << "-c" << "hciconfig -a >> /tmp/temp");

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

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

              @KroMignon said in Cannot get the QProcess started:

              Do you read what other replies to your post?

              I would guess no - the answer was now written three times, the first one already one and a half year ago for the exact same question. Really a nice and much to long active troll.

              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
              1
              • KroMignonK KroMignon

                @AnneRanch said in Cannot get the QProcess started:

                Can anybody help me to solve this?
                Do I have wrong syntax?

                Do you read what other replies to your post?
                For example wat @JonB has written

                to do the same from QProcess you need to go:
                OProcess.start("/bin/sh", QStringList() << "-c" << "hcitool dev >> /tmp/temp");
                // or shorter if you prefer, same thing:
                OProcess.start("/bin/sh", { "-c", "hcitool dev >> /tmp/temp" });

                So this would be: process.start(/bin/sh", QStringList() << "-c" << "hciconfig -a >> /tmp/temp");

                A Offline
                A Offline
                Anonymous_Banned275
                wrote on last edited by
                #12

                @KroMignon OK, I will sincerely apologize for all the trouble I have caused.
                I have made few major mistakes

                1. QProcess is useless without monitoring - hence it is important to utilize as many "connect" as feasible to keep track what QProcess is doing..

                2. In any case process "start" has to be immediately followed - in code - waitForFinished(); Without that the process gets killed and without appropriate "connect" etc etc

                3. I did not read the explanation what Linux and "system" does with "redirecting " AND THAT IS THE MOST IMPORTANT POST in this discussion. I was not aware about the details of "system" call. I look very briefly at the post and did see no point in replacing a simple "system" call , with another application. Many thanks for posting that.

                ... and I think it is a time to close this thread....
                Solved? YES

                KroMignonK JonBJ 2 Replies Last reply
                0
                • A Anonymous_Banned275

                  @KroMignon OK, I will sincerely apologize for all the trouble I have caused.
                  I have made few major mistakes

                  1. QProcess is useless without monitoring - hence it is important to utilize as many "connect" as feasible to keep track what QProcess is doing..

                  2. In any case process "start" has to be immediately followed - in code - waitForFinished(); Without that the process gets killed and without appropriate "connect" etc etc

                  3. I did not read the explanation what Linux and "system" does with "redirecting " AND THAT IS THE MOST IMPORTANT POST in this discussion. I was not aware about the details of "system" call. I look very briefly at the post and did see no point in replacing a simple "system" call , with another application. Many thanks for posting that.

                  ... and I think it is a time to close this thread....
                  Solved? YES

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #13

                  @AnneRanch said in Cannot get the QProcess started:

                  ... and I think it is a time to close this thread....
                  Solved? YES

                  Nice to see you could solve your issue, but I am not really sure that you have understand what you have done wrong.

                  Qt is an asynchronous framework, but some classes can be used in synchronous mode.
                  QProcess is one of them, but you should avoid mixing synchronous and asynchronous calls.

                  That was, for me, the main issue in your code.

                  What I don't understand is why you want to use QProcess to mimic system() calls.
                  For me, using QProcess made only sense when you want to take control about process execution like:

                  • get process exit code
                  • get process output

                  If you only want to start a process and redirect its output to a file, using QProcess is a little bit an "overkill".

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  0
                  • A Anonymous_Banned275

                    @KroMignon OK, I will sincerely apologize for all the trouble I have caused.
                    I have made few major mistakes

                    1. QProcess is useless without monitoring - hence it is important to utilize as many "connect" as feasible to keep track what QProcess is doing..

                    2. In any case process "start" has to be immediately followed - in code - waitForFinished(); Without that the process gets killed and without appropriate "connect" etc etc

                    3. I did not read the explanation what Linux and "system" does with "redirecting " AND THAT IS THE MOST IMPORTANT POST in this discussion. I was not aware about the details of "system" call. I look very briefly at the post and did see no point in replacing a simple "system" call , with another application. Many thanks for posting that.

                    ... and I think it is a time to close this thread....
                    Solved? YES

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

                    @AnneRanch said in Cannot get the QProcess started:

                    In any case process "start" has to be immediately followed - in code - waitForFinished(); Without that the process gets killed and without appropriate "connect" etc etc

                    This is not true. You need to understand the scope of the QProcess variable in your C++ code and declare it in the appropriate place for your desired lifetime.

                    1 Reply Last reply
                    3
                    • JonBJ JonB

                      @AnneRanch said in Cannot get the QProcess started:

                      After all this I realized that I may have a better understanding of QProcess, however, I have a major misunderstanding of trying to replace
                      system("hcotool dev >> /tmp/temp")

                      I may not get much thanks for this, but to answer just this question. You cannot pass >> or >> /tmp/temp as an argument to QProcess. This is because only the Linux shell deals with redirection symbols such as >>. (This would also be the case if you use other redirection symbols such as >, < or |.)

                      When you go system("hcitool dev >> /tmp/temp") what the library function system() does is issue this command: /bin/sh -c "hcitool dev >> /tmp/temp", which gets the /bin/sh shell to execute the command dealing with the >> redirection.

                      To do the same from QProcess you need to go:

                      OProcess.start("/bin/sh", QStringList() << "-c" << "hcitool dev >> /tmp/temp");
                      // or shorter if you prefer, same thing:
                      OProcess.start("/bin/sh", { "-c", "hcitool dev >> /tmp/temp" });
                      

                      That will work.

                      A separate issue is that you would be better not doing it this way and instead handling the output redirection to a file or memory yourself, as @KroMignon shows one way. But if you want to do it the same way as your original system() call this is how to do it via QProcess.

                      A Offline
                      A Offline
                      Anonymous_Banned275
                      wrote on last edited by
                      #15

                      @JonB
                      FYI
                      Contrary to documentation , perhaps it varies with QT version , the attached code (KISS) fills the /temp file - which was my objective .

                           process.startDetached("hcitool dev>>/tmp/temp");
                               process.waitForFinished();
                      
                      KroMignonK JonBJ 2 Replies Last reply
                      0
                      • A Anonymous_Banned275

                        @JonB
                        FYI
                        Contrary to documentation , perhaps it varies with QT version , the attached code (KISS) fills the /temp file - which was my objective .

                             process.startDetached("hcitool dev>>/tmp/temp");
                                 process.waitForFinished();
                        
                        KroMignonK Offline
                        KroMignonK Offline
                        KroMignon
                        wrote on last edited by
                        #16

                        @AnneRanch said in Cannot get the QProcess started:

                        FYI
                        Contrary to documentation , perhaps it varies with QT version , the attached code (KISS) fills the /temp file - which was my objective .

                             process.startDetached("hcitool dev>>/tmp/temp");
                                 process.waitForFinished();
                        

                        Why do you want to use startDetached()?
                        This doesn't make sense to me.
                        startDetached() is for starting a daemon process, even the return PID held by the QProcess instance may become invalid without notice (as written in documentation).
                        https://doc.qt.io/qt-5/qprocess.html#startDetached

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        1 Reply Last reply
                        2
                        • A Anonymous_Banned275

                          @JonB
                          FYI
                          Contrary to documentation , perhaps it varies with QT version , the attached code (KISS) fills the /temp file - which was my objective .

                               process.startDetached("hcitool dev>>/tmp/temp");
                                   process.waitForFinished();
                          
                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #17

                          @AnneRanch said in Cannot get the QProcess started:

                          process.startDetached("hcitool dev>>/tmp/temp");

                          Apart from @KroMignon's correct point that you should not change to startDetached().

                          >> I believe you will find that your statement is not working, and you are mistaken in thinking it does. <<

                          I tested process.startDetached("ls -l >>/tmp/temp"); under Ubuntu 20.04, with Qt 5.12.x. After executing your line, if you look in QT Creator's Application Output pane you get:

                          /usr/bin/ls: cannot access '>>/tmp/temp': No such file or directory
                          

                          Then I tried process.startDetached("hcitool dev>>/tmp/temp"); and the Application Output showed just

                          Devices:
                          

                          In neither case is /tmp/temp created or written to.

                          This is exactly as I would expect and have previously said above: you cannot use a redirection symbol like >> without going via a shell (/bin/sh or /bin/bash followed by -c and then the command). Which is what this whole thread is about. Since you don't, the command (ls or hcitool) sees a single argument of >>/tmp/temp, and cannot act on it correctly.

                          I would guess you had the file /tmp/temp already created from a previous run, filled with previous hcitool output, and thought that meant your command had been successful and appended to it, but it had not. Start by rm /tmp/temp to ENSURE that file does not exist, and then try your startDetached() command again. It does NOT create that file, does it...?

                          1 Reply Last reply
                          2

                          • Login

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