c++ - Qt - Simple example using threads controlled by push buttons -
i have been trying simple example using threads activated pushbuttons work. based off of solution in question below:
how implement frequent start/stop of thread (qthread)
the main differences between example solution above , code below are:
- i used qwidget instead of mainwindow
- i changed name of signals clarity
- my code contains debugging information
- i experimented eliminating signals created worker didn't appear anything
it appears start/stop signals not triggering corresponding slots, not experienced enough troubleshoot why.
additionally, unsure of purpose of signal:
signaltoobj_mainthreadgui()
is used , not?
i have been trying code work time, 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 work"; emit signaltoobj_mainthreadgui(); if (!running || stopped) return; // actual work here /* (int = 0; < 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(); }
you should write
workerthread->start();
or can use thread of threadtest object instead workerthread (in case workerthread needless):
myworker->movetothread(thread()); // this->thread
Comments
Post a Comment