我想在按下按钮 运行 时启动我的 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 没有太多关系,而是与您正在使用的编程语言的语义理解有关。 - Kuba hasn't forgotten Monica