Hello,
I'm trying to launch process using QProcess in QThread, this work with no problem. However, if I check status of the running process, it's always 0 (QProcess::NotRunning).
I attach zipped example which ilustrates the problem, most interesting parts of code:
qmythread.cpp
#include <QProcess>
#include "qmythread.h"
QMyThread
::QMyThread(QObject *parent
){
}
void QMyThread::setParameters(const QString& command)
{
cmd = command;
}
void QMyThread::run()
{
myProcess.execute(cmd);
exec();
}
void QMyThread::teminateProcess()
{
qDebug() << "Attempt to terminate..";
myProcess.terminate();
}
int QMyThread::stateOfProcess()
{
return myProcess.state();
}
#include <QProcess>
#include "qmythread.h"
QMyThread::QMyThread(QObject *parent)
: QThread(parent)
{
}
void QMyThread::setParameters(const QString& command)
{
cmd = command;
}
void QMyThread::run()
{
myProcess.execute(cmd);
exec();
}
void QMyThread::teminateProcess()
{
qDebug() << "Attempt to terminate..";
myProcess.terminate();
}
int QMyThread::stateOfProcess()
{
return myProcess.state();
}
To copy to clipboard, switch view to plain text mode
myqtapp.cpp
#include <QtGui>
#include "myqtapp.h"
#include "qmythread.h"
myQtApp::myQtApp()
{
setupUi(this);
connect( pushButton_launch, SIGNAL( clicked() ), this, SLOT( launch() ) );
connect( pushButton, SIGNAL( clicked() ), this, SLOT( stop() ) );
}
void myQtApp::launch()
{
qDebug() << "Launching program..";
thread = new QMyThread(this);
thread->setParameters("regedit.exe");
thread->start();
}
void myQtApp::stop()
{
if ( thread->isRunning() )
{
qDebug() << "Thread is running..";
qDebug() << "State of process: " << thread->stateOfProcess();
// state of process always returns 0 (QProcess::NotRunning), even if it's running
thread->teminateProcess();
// attempt to terminate process running in thread is unsuccessful
}
else
{
qDebug() << "Error, thread not running";
}
}
#include <QtGui>
#include "myqtapp.h"
#include "qmythread.h"
myQtApp::myQtApp()
{
setupUi(this);
connect( pushButton_launch, SIGNAL( clicked() ), this, SLOT( launch() ) );
connect( pushButton, SIGNAL( clicked() ), this, SLOT( stop() ) );
}
void myQtApp::launch()
{
qDebug() << "Launching program..";
thread = new QMyThread(this);
thread->setParameters("regedit.exe");
thread->start();
}
void myQtApp::stop()
{
if ( thread->isRunning() )
{
qDebug() << "Thread is running..";
qDebug() << "State of process: " << thread->stateOfProcess();
// state of process always returns 0 (QProcess::NotRunning), even if it's running
thread->teminateProcess();
// attempt to terminate process running in thread is unsuccessful
}
else
{
qDebug() << "Error, thread not running";
}
}
To copy to clipboard, switch view to plain text mode
This might be a bug in QT but I don't know if I'm overlooking something or if there is something wrong with my code.
I use Qt 4.2.0-tp1 opensource, mingw, win2000. Thanks for your replies.
Bookmarks