QProcess proc;proc.start(prog, params);
proc.terminate();//proc.kill();
if (proc.state == QProcess::Running) QMessageBox::critical(this, "", "Running");// Выводит Running
namespace Ui {class Controlpanel;}class Controlpanel : public QWidget{ Q_OBJECT public: explicit Controlpanel(QWidget *parent = 0); ~Controlpanel(); QProcess proc;public slots: void readOut(); void exitProc(int, QProcess::ExitStatus);private: Ui::Controlpanel *ui; void init();protected: bool eventFilter(QObject * ,QEvent * );};
Controlpanel::Controlpanel(QWidget *parent) : QWidget(parent), ui(new Ui::Controlpanel){ ui->setupUi(this); init();}void Controlpanel::init(){ this->installEventFilter(this); connect(&proc,SIGNAL(readyReadStandardOutput()),this,SLOT(readOut())); connect(&proc,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(exitProc(int,QProcess::ExitStatus)));}bool Controlpanel::eventFilter(QObject *obj, QEvent *event){ if( event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent*>( event ); switch (keyEvent->key()) { case 48: // "0" if (proc.state() == QProcess::NotRunning) QMessageBox::critical(this, "" , "NOT RUNNING!"); if (proc.state() == QProcess::Running) QMessageBox::critical(this,"","RUNNING!"); if (proc.state() == QProcess::Starting) QMessageBox::critical(this, "", "STARTING!"); if (proc.state() != QProcess::Running) { proc.start("/home/steerage/Steerage", QStringList() << "-qt3"); QMessageBox::critical(this,"","Starting"); } else if (proc.state() == QProcess::Running) { proc.terminate(); //proc.kill(); QMessageBox::critical(this,"","Terminating"); } break; default: break; } } return QObject::eventFilter(obj, event);}void Controlpanel::readOut(){ QByteArray arr; arr = proc.readAllStandardOutput(); if ((QChar(arr[0]) == QChar('4')) && (QChar(arr[1]) == QChar('8'))) proc.terminate(); //proc.kill(); else QMessageBox::critical(this,"",QString(arr));}void Controlpanel::exitProc(int exCode, QProcess::ExitStatus exStat) { QMessageBox::critical(this, "", "The process was finished");}
if (proc.state() != QProcess::Running) { proc.start("/home/steerage/Steerage", QStringList() << "-qt3"); QMessageBox::critical(this,"","Starting"); }