调用quit()后清理QThread

时间:2010-08-16 07:32:11

标签: qt exec signals-slots qthread quit

我有问题。如果我调用Abort(),运行函数将返回而没有complexMath实例有足够的时间来清理。

我想要的是,在调用Abort()之后,complexMath实例有足够的时间自行关闭,在返回之前清除所有挂起的信号和插槽(在complexMath中,它也有自己的信号和插槽)。

void MyThread::Go(){
  start();
}

void MyThread::Abort(){
  emit stopNow();
  quit();
}

void MyThread::run(){
  ComplexMath * complexMath = new ComplexMath();
  connect( complexMath, SIGNAL(OnCalculation(qint)), this, SLOTS(PartialOutput(qint)) );
  connect( this, SIGNAL(stopNow()), complexMath, SLOTS(deleteLater());
  exec();
}

void MyThread::PartialOutput(qint data){
  qDebug() << data;
}

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为你可以摆脱stopNow信号:

void MyThread::Abort(){
  quit();
}

void MyThread::run(){
  ComplexMath * complexMath = new ComplexMath();
  connect( complexMath, SIGNAL(OnCalculation(qint)), this, SLOTS(PartialOutput(qint)) );
  exec();
  // Any code here will be run after the thread quits, and the event loop stops
  deleteLater();
}
相关问题