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. QProcess, What does it do?
Forum Update on Monday, May 27th 2025

QProcess, What does it do?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.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.
  • H Offline
    H Offline
    hjohn
    wrote on last edited by hjohn
    #1

    I've been reading about QProcess and I can't understand why it should be used!
    In the documentation

      QString program = "./path/to/Qt/examples/widgets/analogclock";
      QStringList arguments;
      arguments << "-style" << "fusion";
    
      QProcess *myProcess = new QProcess(parent);
      myProcess->start(program, arguments);  // is this supposed to run the example analogclock ?
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      QProcess is used to start an executable.

      So if ./path/to/Qt/examples/widgets/analogclock is indeed an executable, it should get started and show you something. If that's not the case, then you should check whether it started properly, ended properly, wrote something on its standard output or error channel.

      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
      3
      • H Offline
        H Offline
        hjohn
        wrote on last edited by hjohn
        #3

        @SGaist
        Thanks very much

        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
            QProcess P;
            qDebug()<<"Start the process";
            QString executableName="D:\SmallTryApp"; //Here is the executable which was created using deployment tool
            P.start(executableName);
            qDebug()<<"Let's go!";
             return a.exec();
        }
        

        but it gives me the following output
        Start the process
        Let's go!

        the executable ,i'm expecting to run, is just a QWidget with some funny colour with a label.
        Am I doing something wrong here ?

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

          Several in fact:

          • \ is an escape character when dealing with strings (nothing Qt specific here), so either use \\ or since you are using Qt, using the unix notation for all platform and be done with it.
          • You still don't check that everything is started correctly or if there's something going wrong.

          One last thing to check, can you start SmallTryApp from the Windows explorer ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          H 1 Reply Last reply
          2
          • SGaistS SGaist

            Several in fact:

            • \ is an escape character when dealing with strings (nothing Qt specific here), so either use \\ or since you are using Qt, using the unix notation for all platform and be done with it.
            • You still don't check that everything is started correctly or if there's something going wrong.

            One last thing to check, can you start SmallTryApp from the Windows explorer ?

            H Offline
            H Offline
            hjohn
            wrote on last edited by
            #5

            @SGaist
            Thanks very much , You're right
            I checked it in Linux and it works fine.

            However , in windows it remains as it was. yes SmallTryApp can be started from windows explorer.
            What exactly you mean when you said check that everything is started correctly !!

            one more thing if I might add... Linux and QT are awesome together.

            JonBJ 1 Reply Last reply
            0
            • H hjohn

              @SGaist
              Thanks very much , You're right
              I checked it in Linux and it works fine.

              However , in windows it remains as it was. yes SmallTryApp can be started from windows explorer.
              What exactly you mean when you said check that everything is started correctly !!

              one more thing if I might add... Linux and QT are awesome together.

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

              @hjohn

              However , in windows it remains as it was.

              Show your new Windows code? If you made it work under Linux, there you must have changed the executableName = ... line. When you went back to Windows, did you alter it there too? If not, and it still reads QString executableName="D:\SmallTryApp";, you have not acted on @SGaist's admonition about backslash characters. You will need one of:

              QString executableName="D:\\SmallTryApp"; 
              QString executableName="D:/SmallTryApp"; 
              

              Assuming you actually have a D:\SmallTryApp.exe file which works correctly, one of those will run it.

              What exactly you mean when you said check that everything is started correctly !!

              Most obviously, QProcess::start() does not return a value, but you cannot assume it will have successfully started your sub-process (as you have discovered, for example when the path is wrong). You need to look through the doc page at things like the started signal or the waitForStarted() function. Plus the errorOccurred signal, which you are likely getting. If you do these you would end up with a reason/message if the process could not be successfully run.

              H 1 Reply Last reply
              1
              • JonBJ JonB

                @hjohn

                However , in windows it remains as it was.

                Show your new Windows code? If you made it work under Linux, there you must have changed the executableName = ... line. When you went back to Windows, did you alter it there too? If not, and it still reads QString executableName="D:\SmallTryApp";, you have not acted on @SGaist's admonition about backslash characters. You will need one of:

                QString executableName="D:\\SmallTryApp"; 
                QString executableName="D:/SmallTryApp"; 
                

                Assuming you actually have a D:\SmallTryApp.exe file which works correctly, one of those will run it.

                What exactly you mean when you said check that everything is started correctly !!

                Most obviously, QProcess::start() does not return a value, but you cannot assume it will have successfully started your sub-process (as you have discovered, for example when the path is wrong). You need to look through the doc page at things like the started signal or the waitForStarted() function. Plus the errorOccurred signal, which you are likely getting. If you do these you would end up with a reason/message if the process could not be successfully run.

                H Offline
                H Offline
                hjohn
                wrote on last edited by
                #7

                @JonB
                Thank you.

                The instruction given by @SGaist were strictly followed and the necessary changes were made.
                Linux code is working and so is windows', I can't figure out the mistake but both codes are working good.

                Previously in windows

                qDebug()<<P.state();
                

                and the output was ' QProcess::ProcessState(notRunning)'.
                But now its running.
                So thank you.

                JonBJ 1 Reply Last reply
                0
                • H hjohn

                  @JonB
                  Thank you.

                  The instruction given by @SGaist were strictly followed and the necessary changes were made.
                  Linux code is working and so is windows', I can't figure out the mistake but both codes are working good.

                  Previously in windows

                  qDebug()<<P.state();
                  

                  and the output was ' QProcess::ProcessState(notRunning)'.
                  But now its running.
                  So thank you.

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

                  @hjohn
                  So it sounds like you are saying you have indeed done whatever (one of the two changes I wrote) so that it now works under Windows as well as Linux, right?

                  qDebug()<<P.state();
                  and the output was ' QProcess::ProcessState(notRunning)'.

                  You have put that line immediately after your P.start(). What do you expect? :) QProcess::start() in effect "begins the process of starting the sub-process", and then returns immediately to your calling code. At this point, depending on timing/OS/which way the wind is blowing, your sub-process could be not yet started/started and running/completed and exited --- any of those! So give it a chance --- you'd probably have to let some time elapse before checking the status to see it return running.

                  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