Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to access the OK button in QDialog's buttonBox and use it to stop a thread
Forum Updated to NodeBB v4.3 + New Features

How to access the OK button in QDialog's buttonBox and use it to stop a thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++qt 5.4qdialogqlistwidgetqthread
3 Posts 2 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mr-Workalot
    wrote on 11 Aug 2020, 09:11 last edited by Mr-Workalot 8 Nov 2020, 09:38
    #1

    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 and ok/cancel buttonBox when i click start 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);
        
                
            }
        }
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 Aug 2020, 18:39 last edited by
      #2

      Hi,

      One thing you should do is to wait for the thread to exit in your slot so things shall go as planned.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 11 Aug 2020, 18:41 last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        1/3

        11 Aug 2020, 09:11

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved