- #include "threadcontroller.h" 
- #include "motorcontrollerclasses.h" 
- #include "serversocket.h" 
- #include <QObject> 
- #include <QThread> 
- #include <QDebug> 
-   
- threadController ::threadController(System *- systemHandler,  QObject *- parent )
-     , m_systemHandler(systemHandler) 
- { 
-   
- } 
-   
- void threadController::startMotorThread() //The "spin" motor thread is an example of the infinite loop in a worker thread that is working for me. 
- { 
-     qDebug("threadController startMotorThread() called"); 
-     stopFlag = 0; 
-   
-     if (m_systemHandler->magnetSpinRate() > 0) { 
-         startSpinThread(); 
-         emit userRequestStartSpin(); 
-     } 
-   
- } 
-   
-   
- void threadController::startSpinThread() 
- { 
-     qDebug("startSpinThread() called"); 
-   
-     spinQThread->setObjectName("spinThread"); 
-     motorController *spinMotor = new motorController(*m_systemHandler); 
-     spinMotor->moveToThread(spinQThread); 
-     QObject::connect(- spinMotor,  &- motorController ::destroyMotorThreadSpin- , spinMotor,  &- motorController ::deleteLater)- ; 
 
-     QObject::connect(- spinMotor,  &- motorController ::destroyMotorThreadSpin- , spinQThread,  &- QThread ::quit)- ; 
 
-     QObject::connect(- spinQThread,  &- QThread ::finished- , spinQThread,  &- QThread ::deleteLater)- ; 
 
-     QObject::connect(this- ,  &- threadController ::userRequestStartSpin- , spinMotor,  &- motorController ::controlSpin)- ; 
 
-   
-     spinQThread->start(); 
-   
- } 
-   
- void threadController::stopMotorThread() //stopFlag shuts down ALL of my motors at once - I don't want to use every time for my adjustSweep function (in motorController). 
- { 
-     stopFlag = 1; 
-   
-     qDebug("threadController stopMotorThread() called"); 
-   
- } 
-   
-   
- void threadController::startAdjustSweepMotorThread() //the "adjustSweep" is the one I am having trouble with interrupting and destroying the thread. 
- { 
-     stopAdjustSweepFlag = 0; //stopAdjustSweepFlag is my global var. A value of 1 is supposed to interrupt the sweep. I was noodling around and am using 2 methods to try and stop it - global var and calling a method. 
-     startAdjustSweepThread(); 
-     emit startAdjustSweepSignal(); 
-   
- } 
-   
- void threadController::startAdjustSweepThread() 
- { 
-     qDebug("startAdjustSweepThread called"); 
-   
-     adjustSweepQThread->setObjectName("adjustSweepThread"); 
-     motorController *adjustSweepMotor = new motorController(*m_systemHandler); //(m_systemHandler, this); 
-     adjustSweepMotor->moveToThread(adjustSweepQThread); 
-     QObject::connect(this- ,  &- threadController ::threadStopAdjustSweepSignal- , adjustSweepMotor,  &- motorController ::stopAdjustSweep- , Qt ::QueuedConnection)- ;  //This signal is successfully emitted, but the slot is never called!
 
-     QObject::connect(this- ,  &- threadController ::threadStopAdjustSweepSignal- , adjustSweepMotor,  &- motorController ::test- , Qt ::QueuedConnection)- ;  //Similar story to the above!
 
-     QObject::connect(- adjustSweepMotor,  &- motorController ::destroyMotorThreadAdjustSweep- , adjustSweepMotor,  &- motorController ::deleteLater)- ; 
 
-     QObject::connect(- adjustSweepMotor,  &- motorController ::destroyMotorThreadAdjustSweep- , adjustSweepQThread,  &- QThread ::quit)- ; 
 
-     QObject::connect(- adjustSweepQThread,  &- QThread ::finished- , adjustSweepQThread,  &- QThread ::deleteLater)- ; 
 
-     QObject::connect(this- ,  &- threadController ::startAdjustSweepSignal- , adjustSweepMotor,  &- motorController ::adjustSweep- , Qt ::QueuedConnection)- ; 
 
-   
- //Below is what chatGPT told me to try, I get the signal received statement, but the invokeMethod() doesn't work and can't find the function. 
- //    QObject::connect(this, &threadController::threadStopAdjustSweepSignal, []() { 
- //        qDebug() << "Signal threadStopAdjustSweepSignal received"; 
- //    }); 
-     //QMetaObject::invokeMethod(adjustSweepMotor, "stopAdjustSweep", Qt::QueuedConnection); 
-   
-     adjustSweepQThread->start(); 
- } 
        #include "threadcontroller.h"
#include "motorcontrollerclasses.h"
#include "serversocket.h"
#include <QObject>
#include <QThread>
#include <QDebug>
threadController::threadController(System *systemHandler, QObject *parent)
    : QObject{parent}
    , m_systemHandler(systemHandler)
{
}
void threadController::startMotorThread() //The "spin" motor thread is an example of the infinite loop in a worker thread that is working for me.
{
    qDebug("threadController startMotorThread() called");
    stopFlag = 0;
    if (m_systemHandler->magnetSpinRate() > 0) {
        startSpinThread();
        emit userRequestStartSpin();
    }
}
void threadController::startSpinThread()
{
    qDebug("startSpinThread() called");
    QThread *spinQThread = new QThread(); //Pointer To QThread
    spinQThread->setObjectName("spinThread");
    motorController *spinMotor = new motorController(*m_systemHandler);
    spinMotor->moveToThread(spinQThread);
    QObject::connect(spinMotor, &motorController::destroyMotorThreadSpin, spinMotor, &motorController::deleteLater);
    QObject::connect(spinMotor, &motorController::destroyMotorThreadSpin, spinQThread, &QThread::quit);
    QObject::connect(spinQThread, &QThread::finished, spinQThread, &QThread::deleteLater);
    QObject::connect(this, &threadController::userRequestStartSpin, spinMotor, &motorController::controlSpin);
    spinQThread->start();
}
void threadController::stopMotorThread() //stopFlag shuts down ALL of my motors at once - I don't want to use every time for my adjustSweep function (in motorController).
{
    stopFlag = 1;
    qDebug("threadController stopMotorThread() called");
}
void threadController::startAdjustSweepMotorThread() //the "adjustSweep" is the one I am having trouble with interrupting and destroying the thread.
{
    stopAdjustSweepFlag = 0; //stopAdjustSweepFlag is my global var. A value of 1 is supposed to interrupt the sweep. I was noodling around and am using 2 methods to try and stop it - global var and calling a method.
    startAdjustSweepThread();
    emit startAdjustSweepSignal();
}
void threadController::startAdjustSweepThread()
{
    qDebug("startAdjustSweepThread called");
    QThread *adjustSweepQThread = new QThread(); //Pointer To QThread
    adjustSweepQThread->setObjectName("adjustSweepThread");
    motorController *adjustSweepMotor = new motorController(*m_systemHandler); //(m_systemHandler, this);
    adjustSweepMotor->moveToThread(adjustSweepQThread);
    QObject::connect(this, &threadController::threadStopAdjustSweepSignal, adjustSweepMotor, &motorController::stopAdjustSweep, Qt::QueuedConnection); //This signal is successfully emitted, but the slot is never called!
    QObject::connect(this, &threadController::threadStopAdjustSweepSignal, adjustSweepMotor, &motorController::test, Qt::QueuedConnection); //Similar story to the above!
    QObject::connect(adjustSweepMotor, &motorController::destroyMotorThreadAdjustSweep, adjustSweepMotor, &motorController::deleteLater);
    QObject::connect(adjustSweepMotor, &motorController::destroyMotorThreadAdjustSweep, adjustSweepQThread, &QThread::quit);
    QObject::connect(adjustSweepQThread, &QThread::finished, adjustSweepQThread, &QThread::deleteLater);
    QObject::connect(this, &threadController::startAdjustSweepSignal, adjustSweepMotor, &motorController::adjustSweep, Qt::QueuedConnection);
//Below is what chatGPT told me to try, I get the signal received statement, but the invokeMethod() doesn't work and can't find the function.
//    QObject::connect(this, &threadController::threadStopAdjustSweepSignal, []() {
//        qDebug() << "Signal threadStopAdjustSweepSignal received";
//    });
    //QMetaObject::invokeMethod(adjustSweepMotor, "stopAdjustSweep", Qt::QueuedConnection);
    adjustSweepQThread->start();
}
To copy to clipboard, switch view to plain text mode 
  
Bookmarks