About QProcess::pid() function



  • 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?



  • 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



  • 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



  • 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.


  • 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]



  • 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@



  • "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.



  • 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



  • [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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.