0

I have been trying to get this simple example using threads activated by pushbuttons to work. It is based off of the solution in the question below:

How to implement frequent start/stop of a thread (QThread)

The main differences between the example solution above and my code below are:

  1. I used a QWidget instead of MainWindow
  2. I changed the name of signals for clarity
  3. My code contains debugging information
  4. I experimented with eliminating the signals created by worker as the didn't appear to do anything

It appears that the start/stop signals are not triggering their corresponding slots, but I am not experienced enough to troubleshoot why.

Additionally, I am unsure of the purpose of the signal:

SignalToObj_mainThreadGUI()

Is that just something that could be used and is not?

I have been trying to get this code to work for some time, so any help would be greatly appreciated.

main.cpp

#include "threadtest.h"
#include <QApplication>

int main(int argc, char *argv[]) 
{
    QApplication a(argc, argv);
    ThreadTest w;
    w.show();

    return a.exec(); 
}

threadtest.h

#include <QWidget>
#include <QThread>
#include "worker.h"     

namespace Ui 
{
    class ThreadTest;
}

class ThreadTest : public QWidget
{
    Q_OBJECT

public:
    explicit ThreadTest(QWidget *parent = 0);
    ~ThreadTest();

signals:
    void startWorkSignal();
    void stopWorkSignal();


private slots:
    void on_startButton_clicked();
    void on_stopButton_clicked();

private:
    Ui::ThreadTest *ui;
    worker *myWorker;
    QThread *WorkerThread;
};

threadtest.cpp

#include "threadtest.h"
#include "ui_threadtest.h"

ThreadTest::ThreadTest(QWidget *parent) :
QWidget(parent),
ui(new Ui::ThreadTest) 
{
    ui->setupUi(this);

    myWorker = new worker;
    WorkerThread = new QThread;
    myWorker->moveToThread(WorkerThread);

    connect(this,
            SIGNAL(startWorkSignal()),
            myWorker,
            SLOT(StartWork())
            );

    connect(this,
            SIGNAL(stopWorkSignal()),
            myWorker,
            SLOT(StopWork())
            );

    //Debug
    this->dumpObjectInfo();
    myWorker->dumpObjectInfo(); 
}

ThreadTest::~ThreadTest() 
{
    delete ui; 
}

void ThreadTest::on_startButton_clicked() 
{
    qDebug() << "startwork signal emmitted";
    emit startWorkSignal(); 
}

void ThreadTest::on_stopButton_clicked() 
{
    qDebug() << "stopwork signal emmitted";
    emit stopWorkSignal(); 
}

worker.h

#include <QObject>
#include <QDebug>

class worker : public QObject {
    Q_OBJECT 
public:
    explicit worker(QObject *parent = 0);
    ~worker();

signals:
    void SignalToObj_mainThreadGUI();
    //void running();
    //void stopped();

public slots:
    void StopWork();
    void StartWork();

private slots:
    void do_Work();

private:
    volatile bool running, stopped; 
};

worker.cpp

#include "worker.h"

worker::worker(QObject *parent) : QObject(parent), stopped(false),
running(false) 
{
    qDebug() << "running: " << running;
    qDebug() << "stopped: " << stopped; 

}


worker::~worker() {}

void worker::do_Work() 
{
    qDebug() << "inside do Work";
    emit SignalToObj_mainThreadGUI();

    if (!running || stopped) return;

    // actual work here
    /*
    for (int i = 0; i < 100; i++)
    {
        qDebug() << "count: " + i;
    }
    */
    QMetaObject::invokeMethod(this, "do_Work", Qt::QueuedConnection); 
}

void worker::StopWork() 
{
    qDebug() << "inside StopWork";
    stopped = true;
    running = false;

    //emit stopped(); 
}

void worker::StartWork() 
{
    qDebug() << "inside StartWork";
    stopped = false;
    running = true;

    //emit running();
    do_Work(); 
}
Community
  • 1
  • 1
Langleson
  • 103
  • 1
  • 13

2 Answers2

1

The slots are not triggered, because you have moved myWork to the thread WorkerThread, but didnot run an event loop in that thread. In threadtest.cpp, add

WorkerThread .start();

after

myWorker = new worker;
WorkerThread = new QThread;
myWorker->moveToThread(WorkerThread);
Yang Kui
  • 548
  • 5
  • 11
1

You should write

WorkerThread->start();

Or you can use the thread of the ThreadTest object instead the WorkerThread (in this case the WorkerThread is needless):

 myWorker->moveToThread(thread()); // this->thread