I would like to start my QThread
when I push on button Run. But the compiler outputs following error:
QThread: Destroyed while thread is still running
ASSERT failure in QThread::setTerminationEnabled(): "Current thread was not started with QThread.", file thread\qthread_win.cp.
I don't know what is wrong with my code.
Any help would be appreciated.
Here is my code:
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
is created in the first line ofMainWindow::start
, then started, then immediately destroyed while it is still running as thestart
returns. The error message tells you what's wrong, and C++ semantics tell you why it's so. This question has not much to do with Qt, all to do with understanding of the semantics of the programming language that you are using. – Acclamation