Skip to content

Instantly share code, notes, and snippets.

@micjabbour
Created January 17, 2017 14:02
Show Gist options
  • Save micjabbour/508e78562a5a28b3e9047d17fc342171 to your computer and use it in GitHub Desktop.
Save micjabbour/508e78562a5a28b3e9047d17fc342171 to your computer and use it in GitHub Desktop.
Worker QObjects - QThread::quit() while there are events in the event queue test
#include <QtWidgets>
//QThread wrapper for safe destruction
//see http://stackoverflow.com/a/19666329
class Thread : public QThread {
using QThread::run; // final
public:
Thread(QObject * parent = 0) : QThread(parent) {}
~Thread() { quit(); wait(); }
};
class Producer : public QObject{
Q_OBJECT
public:
explicit Producer(QObject* parent= nullptr):QObject(parent){
timer.setInterval(250);
QObject::connect(&timer, &QTimer::timeout, [this]{
emit ProducedNewCount(counter++);
});
timer.start();
}
~Producer() = default;
Q_SIGNAL void ProducedNewCount(int counter);
private:
int counter = 0;
QTimer timer{this};
};
class Consumer : public QObject{
Q_OBJECT
public:
explicit Consumer(QObject* parent= nullptr):QObject(parent){}
~Consumer() = default;
Q_SLOT void ConsumeCount(int counter){
emit ConsumingCount(counter);
//some heavy operation to make consumer slower than producer
QThread::sleep(1);
}
Q_SIGNAL void ConsumingCount(int counter);
};
int main(int argc, char* argv[]){
QApplication a(argc, argv);
Producer* workerProducer = new Producer();
Thread threadProducer;
QObject::connect(&threadProducer, &Thread::finished,
workerProducer, &QObject::deleteLater);
workerProducer->moveToThread(&threadProducer);
threadProducer.start();
Consumer* workerConsumer = new Consumer();
Thread threadConsumer;
QObject::connect(&threadConsumer, &Thread::finished,
workerConsumer, &QObject::deleteLater);
workerConsumer->moveToThread(&threadConsumer);
threadConsumer.start();
QObject::connect(workerProducer, &Producer::ProducedNewCount,
workerConsumer, &Consumer::ConsumeCount);
//set up GUI
QWidget widget;
QVBoxLayout vLayout(&widget);
QTextEdit textEdit;
QHBoxLayout hLayout;
QPushButton buttonQuitProducer("Quit Producer Thread");
QPushButton buttonQuitConsumer("Quit Consumer Thread");
hLayout.addWidget(&buttonQuitProducer);
hLayout.addWidget(&buttonQuitConsumer);
vLayout.addWidget(&textEdit);
vLayout.addLayout(&hLayout);
QObject::connect(workerProducer, &Producer::ProducedNewCount,
&textEdit, [&](int counter){
textEdit.append(QStringLiteral("<font color=\"red\">Producer:\tNew Counter %1</p>").arg(counter));
});
QObject::connect(workerConsumer, &Consumer::ConsumingCount,
&textEdit, [&](int counter){
textEdit.append(QStringLiteral("<font color=\"blue\">Consumer:\tNew Counter %1</p>").arg(counter));
});
QObject::connect(&buttonQuitProducer, &QPushButton::clicked,
&threadProducer, &Thread::quit);
QObject::connect(&buttonQuitConsumer, &QPushButton::clicked,
&threadConsumer, &Thread::quit);
widget.show();
return a.exec();
}
#include "main.moc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment