【发布时间】:2014-09-18 01:20:48
【问题描述】:
当我按下按钮运行时,我想启动我的QThread。但是编译器输出如下错误:
QThread: Destroyed while thread is still running
ASSERT failure in QThread::setTerminationEnabled(): "Current thread was not started with QThread.", file thread\qthread_win.cp.
我不知道我的代码有什么问题。
任何帮助将不胜感激。
这是我的代码:
SamplingThread::SamplingThread( QObject *parent):
QwtSamplingThread( parent ),
d_frequency( 5.0 )
{
init();
}
MainWindow::MainWindow( QWidget *parent ):
QMainWindow( parent )
{.......
.....
run= new QPushButton ("Run",this);
stop= new QPushButton("Stop",this);
connect(run, SIGNAL(clicked()),this, SLOT (start()));
}
MainWindow::start
{
SamplingThread samplingThread;
samplingThread.setFrequency( frequency() );
samplingThread.start();
}
int main( int argc, char **argv )
{
QApplication app( argc, argv );
MainWindow window;
window.resize( 700, 400 );
window.show();
bool ok = app.exec();
return ok;
}
【问题讨论】:
-
SamplingThread在MainWindow::start的第一行创建,然后启动,然后在start返回时立即销毁当它仍在运行时。错误消息告诉你出了什么问题,C++ 语义告诉你为什么会这样。这个问题与 Qt 没有太大关系,全都与您对所使用的编程语言的语义的理解有关。
标签: c++ qt signals-slots qthread qwt