QObject:无法为处于不同线程的父级创建子级。另外,QProcess

时间:2017-02-03 15:26:50

标签: qt qthread

我是QThread的新手我有一个应用程序,我们可以从Class" buildScript"中运行Qthread :: run()。 BuildScriptThread类继承自Qthread。

我必须运行线程直到它满足某些条件,例如 在BuildScriptThread类的run()中使用(untilfalse())。

内部虽然我必须通过一些参数传递调用QProcess来执行某些命令。在run()中。使用输出击打5,6次应用程序崩溃后的一些时间:

  

警告:QObject:无法为a中的父级创建子级   不同的线程。 (父是QProcess(0x2f8cb00),父亲的线程是   QThread(0x698198),当前线程是BuildScriptThread(0x6d40e8)

所以请看看:::

类BuildScriptThread头文件

class BuildScriptThread : public QThread
{
Q_OBJECT

private:

QProcess *buildProc;    
QString buildDirectory;


private slots:
void readStdOutput();

 public:
BuildScriptThread(MainWindow *mainWin);
~BuildScriptThread();
void CurrentXmlModule(QString module);

public:
void run();

QStringList BuildPrameter();
QString killCurrentProcess();
bool isAllListEmpty();
void interruptibleCall();

   protected:
   void interruptible();
   signals:
   void updateStatus(QString );
   void buildScriptCompleted();

   };

   BuildScriptThread::BuildScriptThread(MainWindow *mainWin)
   {
    stopped = false;
    mainWindow=mainWin;
    buildProc = new QProcess();
     xmlModuleStruct.verModuleFlag =false;
    //Define slot for ready standard output data signal
     connect(buildProc, SIGNAL(readyReadStandardOutput()), this, SLOT(readStdOutput()));
     }

     BuildScriptThread::~BuildScriptThread()
    {
   //Disconnect the ready standard output data signal
    disconnect(buildProc, SIGNAL(readyReadStandardOutput()), this,    SLOT(readStdOutput()));
    delete buildProc;
   buildProc = NULL;
    }

         void BuildScriptThread::run()
        {

         QString setdirectory = mainWindow->getDirectory();
         setdirectory.remove(setdirectory.indexOf("/veri"),13);
         QDir::setCurrent(setdirectory);

         while(!isAllListEmpty()){
            QThread::sleep(1);
            qDebug() << killCurrentProcess();
            buildProc->setProcessChannelMode(QProcess::MergedChannels);
            QStringList buildPram = QStringList() <<  BuildPrameter();
             QThread::usleep(500);
            buildProc->start("script.bat",buildPram);
            if (!buildProc->waitForFinished(-1))
            {
                QString errMsg = buildProc->errorString().append(": ").append(buildScriptName);
                emit updateStatus(errMsg);
            }
            QThread::usleep(500);
        }
         emit buildScriptCompleted();
        QDir::setCurrent(currentPath);
         }



        void BuildScriptThread::readStdOutput()
         {

          QByteArray data = buildProc->readAllStandardOutput();  
         }
           QString BuildScriptThread::killCurrentProcess(){

         int PID = buildProc->processId();
          if(PID > 0){   
         QString str1 = QString("taskkill /PID %1 /f").arg(PID);
         QProcess::execute(str1);
        return "Terminating Thread";
        }else{
       qDebug() << "PID is Zero";
        return "Process Is not Running";
        }
      delete buildProc;
      buildProc = NULL;
     buildProc = new QProcess();
    }

    bool BuildScriptThread::isAllListEmpty()
  {

     return (xmlModuleStruct.List1.isEmpty() &       xmlModuleStruct.List2.isEmpty() & xmlModuleStruct.List2.isEmpty());
   }

  void BuildScriptThread::interruptibleCall()
  {
 interruptible();
  }

 void BuildScriptThread::interruptible()
 {
this->setTerminationEnabled(false);
 }

标头文件

    class BuildScript : public QObject
   {
    Q_OBJECT

    public:
   BuildScript(QObject* parent=0, MainWindow *minWin=0);
  ~BuildScript();

   void startRun();


    private:
    BuildScriptThread *buildScriptThread;
    MainWindow *mainWindow;

    signals:
    void buildScriptCompleted();
    void updateStatus(QString);
    void UpdateWindowtile();

    public slots:
    void abortThread();

};

源文件

     BuildScript::BuildScript(QObject *parent, MainWindow *minWin)
 :QObject (parent)
       {
       mainWindow = minWin;
       buildScriptThread = new BuildScriptThread(minWin);
       buildScriptThread->interruptibleCall();
              connect(buildScriptThread,SIGNAL(updateStatus(QString)),this,SIGNAL(updateStatus(QString)));
     connect(buildScriptThread, SIGNAL(buildScriptCompleted()),this, SIGNAL(buildScriptCompleted()));
connect(mainWindow,SIGNAL(abortThread()),this,SLOT(abortThread()));
       }

     BuildScript::~BuildScript()
     {
          disconnect(buildScriptThread,SIGNAL(updateStatus(QString)),this,SIGNAL(updateStatus(QString)));
      disconnect(buildScriptThread, SIGNAL(buildScriptCompleted()),this, SIGNAL(buildScriptCompleted()));
      }

     void BuildScript::startRun
     {
       buildScriptThread->start();
     }

  void BuildScript::abortThread()
  {
  qDebug() << "Terminating";
   if(buildScriptThread->isRunning()){

  qDebug() << buildScriptThread->killCurrentProcess();
   buildScriptThread->terminate();
   emit buildScriptCompleted();
   }else{
   emit UpdateWindowtile();
   }
   }

0 个答案:

没有答案