How to access the OK button in QDialog's buttonBox and use it to stop a thread
-
I have a separate function running in my MyThread class, this running of this function can be gracefully stopped if
bool *stop;
declared in MyThread.h is set to true. This thread is used to populate round 10000 items in my qlistwidget in my qdialog box.and in my dialogbox,i have
start button
,stop button
,qlistwidget andok/cancel buttonBox
when i clickstart button
the thread runs and starts adding items in my dialog's qlistwidget, and when i clickstop button
, the thread stops(sometimes i want to stop it before all 10000 items are loaded) and the stops adding more items in my qlistwidget,but i also want it to stop when i click ok button in my buttonbox.and dialog should also disappear after i click ok (as expected normal behaviour)
Mythread.cpp
**Inside some function emit test_sending(Qstrint which will be added in my QDialog's Qlistwidget eventually);
Dialog.h
MyThread* mThread;
Dialog.cpp
mThread = new MyThread(this); connect(mThread, SIGNAL(test_sending(QString)), this, SLOT(on_test_sending(QString))); void KeyComd::on_test_sending(QString string) { ui.dialogs_listwidget->addItem(string); }
The thread will start when i click start button
void Dialog::on_start_clicked() { //started mThread->start(); }
The thread will stop when i click stop button
void KeyComd::on_stop_clicked() //stop button { *mThread->stop = true; //stop is a pointer boolen object, declared as bool *stop; in MyThread.h }
Approach-1 i tried
void KeyComd::on_buttonBox_accepted()//Ok button { *mThread->stop = true; //This doesn't work }
the UI just crashes giving me Fatal exit requested.
`Approach-2 i tried
void Dialog::accept() { *mThread->stop = true; }
This stops the running of my thread , but upon clicking 'Ok' the dialog box doesnt disappear as it should.
for more information these are the connections in my Dialog.ui xml file
<connection> <sender>buttonBox</sender> <signal>accepted()</signal> <receiver>DialogK</receiver> <slot>accept()</slot> <hints> <hint type="sourcelabel"> <x>248</x> <y>254</y> </hint> <hint type="destinationlabel"> <x>157</x> <y>274</y> </hint> </hints> </connection> <connection> <sender>buttonBox</sender> <signal>rejected()</signal> <receiver>DialogK</receiver> <slot>reject()</slot> <hints> <hint type="sourcelabel"> <x>316</x> <y>260</y> </hint> <hint type="destinationlabel"> <x>286</x> <y>274</y> </hint> </hints> </connection> </connections>```
more relevant code mythread.cpp
void MyThread::run() { this->stop = &(this->value); //assigning false value to stop initially Print_Descendants_key(exc_1.pUIAutomation, nullptr,this->stop,0); //calling this function } void MyThread::Print_Descendants_key(IUIAutomation* pUIAutomation, IUIAutomationElement* pParent, bool* stop, int indent) { if (*stop == true) return; ///create QString txt while (pNode && (*stop == false)) { emit test_sending(txt); Print_Descendants_key(pUIAutomation, pNode,stop ,indent + 1); } }
-
Hi,
One thing you should do is to wait for the thread to exit in your slot so things shall go as planned.
-
By the way, how are your buttons connected ?
You might want to rather call the dialog's accept slot from on_buttonBox_accepted rather than connecting it directly to your button.