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. About QProcess::pid() function
Forum Updated to NodeBB v4.3 + New Features

About QProcess::pid() function

Scheduled Pinned Locked Moved General and Desktop
9 Posts 6 Posters 26.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.
  • I Offline
    I Offline
    ice2000feng
    wrote on last edited by
    #1

    1.I use QProcess::pid() to get pid of starting program, but the pid value which I got is 0, I don't know why?
    my codes is bellow:

    @
    QProcess *newProcess = new QProcess(this),
    newProcess->startDetached(myExtenalProgram);
    int tmpPid = newProcess->pid
    cout << tmpPid << endl;
    @

    the value of the tmpPid is 0.
    why the value of the tmpPid is 0?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #2

      From the "documentation":http://doc.qt.nokia.com/4.7/qprocess.html#pid :
      [quote]Returns the native process identifier for the running process, if available. If no process is currently running, 0 is returned.[/quote]
      "This":http://doc.qt.nokia.com/4.7/qprocess.html#waitForStarted should do it for you.

      Edit: BTW, please use @ to wrap code

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ice2000feng
        wrote on last edited by
        #3

        the code is bellow:

        @
        MainWindow::MainWindow
        {
        connect(button1, SIGNAL(clicked()), this, SLOT(startProcess());
        //define newProcess in the MainWindow.h, QProcess *newProcess
        newProcess = new QProcess(this)
        ........
        connect(button2, SIGNAL(clicked()), this, SLOT(getPid());
        }

        void MainWindow::startProcess()
        {
        ........
        newProcess->startDetached("firefox", sList, workDir, &pid);
        ........
        }

        void MainWindow::getPid()
        {
        ........
        QFile file("wid.txt");
        if (!file.open(QIODevice::Append))
        {
        return;
        }

        QTextStream out(&file);
        out << "the firefox pid:" << newProcess->pid() << endl;
        

        }
        @

        EDIT: please use @-tags to wrap your code, Gerolf

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          A number of things could be going wrong:

          • Perhaps you're too fast. You should wait for the signal from QProcess that the process has started before you try to get the PID
          • Perhaps the firefox process is not really the process you're after, but only a startup script. In that case, the process you started would be dead again already before you even got to ask it's PID.
          1 Reply Last reply
          0
          • JeroentjehomeJ Offline
            JeroentjehomeJ Offline
            Jeroentjehome
            wrote on last edited by
            #5

            Maybe try to use this
            @Q_PID pidtemp = newProcess->pid;
            @
            Or when using the StartDetached () get the pid directly.
            @bool QProcess::startDetached ( const QString & program, const QStringList & arguments, const QString & workingDirectory, qint64 * pid = 0 )
            @

            [Edit: Please, use @-tags for code snippets /Vass]

            Greetz, Jeroen

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fluca1978
              wrote on last edited by
              #6

              I tried this simple example and it is not working too:

              @Runner::Runner(QObject *parent) :
              QObject(parent)
              {
              firefox = new QProcess( this );

              connect(firefox,
                      SIGNAL(started()),
                      this,
                      SLOT(slotProcessStarted()));
              
              
              qDebug() << "Starting firefox..";
              bool started = firefox->startDetached( "firefox" );
              qDebug() << "Process started:" << started;
              

              }

              void Runner::slotProcessStarted()
              {
              qDebug()<< "**** Process started with pid " << firefox->pid();
              }@

              The program prints:

              @Process started: true @

              but it seems it never enters the started slot. In fact the QProcess documentation does not specifies that a started signal is emitted for a startDetached call. If you change the above code with the following:

              @Runner::Runner(QObject *parent) :
              QObject(parent)
              {
              firefox = new QProcess( this );

              connect(firefox,
                      SIGNAL(started()),
                      this,
                      SLOT(slotProcessStarted()));
              
              
              
              qDebug() << "Starting firefox..";
              firefox->start( "firefox" );
              qDebug() << "Process started";
              

              }
              @

              then you will see:
              @Starting firefox..
              Process started
              **** Process started with pid 8328@

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                "QProcess::startDetached() ":http://doc.qt.nokia.com/4.7/qprocess.html#startDetached are static methods and therefore never send signals.

                And this leads to the next explanation for your problem:
                As your QProcess object is never associated with an actual process, it does not hold a valid PID, of cours. You called a static method on an object, remeber? While this is legal, it does of course not change your object.

                So, if you need the PID, use the overload that takes an qint64 pointer as argument, as already pointed out by Jeroentje@job.

                http://www.catb.org/~esr/faqs/smart-questions.html

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

                  Hi,
                  The start of a process is easy ;-) getting the pid a piece of cake, but how can I control that external process window using that pid?
                  That is something I'm wondering about. Not real much to find in this forum :-( Got any ideas Volker?
                  greetz

                  Greetz, Jeroen

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    [quote author="Jeroentje@job" date="1317726501"]
                    That is something I'm wondering about. Not real much to find in this forum :-( Got any ideas Volker?
                    greetz[/quote]

                    You asked this "here":http://developer.qt.nokia.com/forums/viewthread/10307/ already, so please wait for an answer over there and don't mix topics here. Thanks.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    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