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. [SOLVED] QProcess exits a few seconds after starting

[SOLVED] QProcess exits a few seconds after starting

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 2.7k 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.
  • R Offline
    R Offline
    rizoritis
    wrote on last edited by
    #1

    Hello,

    I uses QProcess to start a new process as with the code below. I know my spawned process works fine when I run it independent of the Qt GUI. For some reason when I try to start the process from my Qt GUI, it starts and runs for about a second then just exits. The error code I get from waitForStart is "2: The last waitFor...() function timed out. The state of QProcess is unchanged, and you can try calling waitFor...() again". Any thoughts? Also, the funny thing is if I use start() instead of startDetached() the program doesnt even start. Maybe someone knows why this may be as well?

    @ QObject *parent;
    QString program = ""C:\Documents and Settings\Ryan\My Documents..."";
    QString workingDir = ""C:\Documents and Settings\Ryan\My Documents..."";
    QProcess *controllerProcess = new QProcess(parent = 0);
    controllerProcess->setWorkingDirectory(workingDir);
    controllerProcess->startDetached(program);
    if(!controllerProcess->waitForStarted(10000))
    {
    qDebug() << "Controller process failed to start!";
    qDebug() << "Process State: " << controllerProcess->state() << " " << "Error Code: " << controllerProcess->error() << " " << "Exit Status: " << controllerProcess->exitStatus();
    return -1;
    }@

    Thanks in advance!

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Hmm,
      Because we are unable to really test the code it's a bit guessing what's the problem.
      The startDetached should work like you see. The problem might be that your controllerProcess is unallocated when your functions end. Qt is smart enough to also kill your other program with it.
      The waitForStarted() might actually signal a proper start of your other program, but get's destroyed because of the earlier mentioned issue. Make sure your QProcess variable is a class member and active when your external program needs to run.
      Greetz

      Greetz, Jeroen

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rizoritis
        wrote on last edited by
        #3

        I made it a member of the class now. Here is my code:

        program.c->
        @BasicGui::BasicGui(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::BasicGui)
        {
        ui->setupUi(this);

        createControllerProcess();
        

        }

        BasicGui::~BasicGui()
        {
        delete ui;
        }

        void BasicGui::createSharedMemory()
        {
        int size = sizeof(double);

        if(sharedMemory.isAttached()) {sharedMemory.detach();}
        
        sharedMemory.setKey("SharedMemory");
        
        if(!sharedMemory.create(size, QSharedMemory::ReadWrite))
        {
            qDebug() << "Unable to create shared memory segment!";
            qDebug() << "Error code: " << sharedMemory.errorString();
        }
        

        }

        void BasicGui::createControllerProcess()
        {
        QObject *parent;
        QString program = ""C:\Documents and Settings..."";
        QString workingDir = ""C:\Documents and Settings..."";

        controllerProcess = new QProcess(parent = 0);
        controllerProcess->setWorkingDirectory(workingDir);
        controllerProcess->startDetached(program);
        
        Sleep(5000);
        

        }
        @

        program.h->
        @namespace Ui {
        class BasicGui;
        }

        class BasicGui : public QMainWindow
        {
        Q_OBJECT

        public:
        explicit BasicGui(QWidget *parent = 0);
        ~BasicGui();
        QSharedMemory sharedMemory;
        QProcess *controllerProcess;

        private:
        Ui::BasicGui *ui;
        void createSharedMemory();
        void createControllerProcess();
        };

        #endif // BASICGUI_H
        @

        I am thinking that it could be something with losing scope or memory. However, I put a sleep after starting the process and it still ended early before we were even out of the function so I'm not so sure it is scope.

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

          Hi,

          startDetached is a static function, your usage of it is wrong

          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
          • R Offline
            R Offline
            rizoritis
            wrote on last edited by
            #5

            [quote author="SGaist" date="1369829265"]Hi,

            startDetached is a static function, your usage of it is wrong[/quote]

            I am a bit confused on how I should use it. Any suggestions?

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              [quote author="rizoritis" date="1369830356"]
              [quote author="SGaist" date="1369829265"]Hi,

              startDetached is a static function, your usage of it is wrong[/quote]

              I am a bit confused on how I should use it. Any suggestions?[/quote]

              Checkout the documentation of "startDetached":http://qt-project.org/doc/qt-5.0/qtcore/qprocess.html#startDetached
              AFAIK you should use the parameter list for the working dir.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • JeroentjehomeJ Offline
                JeroentjehomeJ Offline
                Jeroentjehome
                wrote on last edited by
                #7

                Static functions are called:
                @QProcess::startDetached(..)@
                instead of
                @m_process.startDetached(..)@
                Greetz

                Greetz, Jeroen

                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