2020-06-11 02:14:54 +00:00
|
|
|
#ifndef FUNCTIONTHREAD_H
|
|
|
|
#define FUNCTIONTHREAD_H
|
|
|
|
|
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief The LambdaThread class
|
|
|
|
*
|
|
|
|
* Provides a QThread which performs a supplied lambda before kicking off its event loop.
|
|
|
|
*
|
|
|
|
* Disclaimer: this might be a crutch that reveals a misunderstanding of the Qt
|
|
|
|
* threading infrastructure. We'll see.
|
|
|
|
*/
|
|
|
|
class FunctionThread: public QThread {
|
|
|
|
public:
|
|
|
|
FunctionThread() : QThread() {}
|
|
|
|
|
|
|
|
void setFunction(const std::function<void(void)> &function) {
|
|
|
|
this->function = function;
|
|
|
|
}
|
|
|
|
|
|
|
|
void run() override {
|
|
|
|
function();
|
|
|
|
exec();
|
|
|
|
}
|
|
|
|
|
2020-06-15 03:38:44 +00:00
|
|
|
void stop() {
|
2020-06-15 04:00:44 +00:00
|
|
|
// QMetaObject::invokeMethod(this, "quit", Qt::QueuedConnection);
|
|
|
|
quit();
|
|
|
|
wait();
|
2020-06-15 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 02:14:54 +00:00
|
|
|
private:
|
|
|
|
std::function<void(void)> function;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // FUNCTIONTHREAD_H
|