Home > other >  Why qt pause button to close the open thread?
Why qt pause button to close the open thread?

Time:02-19

The following is the main interface code:
 
New_qingsha_by_qt: : new_qingsha_by_qt (QWidget * parent)
: QMainWindow (parent)
{

The UI - & gt; SetupUi (this);
QPixmap pixmap ("./PIC/logo. PNG ");
The UI - & gt; Icon - & gt; SetPixmap (pixmap);
//parameter 1 signal sender (pointer), a signal sent by a parameter 2 (address) signal, parameters signal receiver (pointer) 3, 4 signal processing function (function addresses)
//connect (UI - & gt; CloseBtn, & amp; QPushButton: : clicked, this, & amp; New_qingsha_by_qt: : close);
The connect (UI - & gt; CloseBtn, SIGNAL (clicked ()), and this, SLOT (on_close_clicked ()));
The connect (UI - & gt; StartBtn, SIGNAL (clicked ()), and this, SLOT (on_start_clicked ()));
The connect (UI - & gt; PushButton, SIGNAL (clicked ()), and this, SLOT (on_clear_clicked ()));
The connect (UI - & gt; PauseBtn, SIGNAL (clicked ()), and this, SLOT (on_pause_clicked ()));

//thread initialization
M_thread=new MyThread;
M_childThread=new QThread;//the child thread itself is not responsible for handling

M_thread - & gt; MoveToThread (m_childThread);//move the instance to the new thread, realize multithreading running

The connect (this, SIGNAL (startThread ()), m_thread, SLOT (startHandle ()));

The connect (this, SIGNAL (stopThread ()), m_thread, SLOT (cancelHandle ()));

The connect (m_thread, SIGNAL (callFather (void)), and this, SLOT (closeThread ()));
M_childThread - & gt; Start ().//promoter thread
}
New_qingsha_by_qt: : ~ new_qingsha_by_qt ()
{
Delete the UI;
}


Void new_qingsha_by_qt: : on_close_clicked ()
{
QApplication: : the exit ();
}


Void new_qingsha_by_qt: : on_start_clicked ()
{
QDebug () & lt; <"Main Thread:" & lt; QDebug () & lt; <"Thread start";
Emit startThread ();
//UI - & gt; StartBtn - & gt; SetEnabled (false);

}

Void new_qingsha_by_qt: : closeThread ()
{
QDebug () & lt; <"Shutdown".
}



Void new_qingsha_by_qt: : on_clear_clicked ()
{
The UI - & gt; Bad_pic - & gt; The clear ();
The UI - & gt; TextEdit - & gt; The clear ();
The UI - & gt; The result - & gt; The clear ();
}

Void new_qingsha_by_qt: : on_pause_clicked ()
{
QDebug () & lt; <"Thread stop";
Emit stopThread ();//this without triggering
//emit startThread (0);

}


Here are specific multithreaded slot function implementation:
 
MyThread: : MyThread (QObject * parent) : QObject (parent)
{
M_bRun=false;
}

MyThread: : ~ MyThread ()
{

}

Void MyThread: : startHandle ()
{
QDebug () & lt; <"Sub thread:" & lt; For (;; )
{
QThread: : sleep (10);
QDebug () & lt; <"IsRuning";
{
//QMutexLocker locker (& amp; M_Mutex);
If (m_bRun)
{
Emit callFather ();
break;
}
}
}
}


Void MyThread: : cancelHandle ()
{
QDebug () & lt; <"Sub thread" & lt; QDebug () & lt; <"CancelHandle thread";
M_bRun=true;
}



Mainly by m_bRun marks a change to change the thread to run, but after click the pause button, found the groove function and not triggered, the thread has been in open state, could you tell me how to deal with this?
  • Related