12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- #ifndef ONETHREAD_H
- #define ONETHREAD_H
- #include <QObject>
- #include <QTimer>
- #include <functional>
- #include <QThread>
- /**
- * 这个类提供了一种方便的线程调用方法,可以直接用信号将需要放入新线程
- * 运行的函数传递给这个类,然后就会在槽函数中运行这个函数
- *
- */
- class OneThread : public QObject
- {
- Q_OBJECT
- public:
- explicit OneThread(QThread* thread, QObject* parent = nullptr);
- ~OneThread();
- void startThread();
- /* 将函数与参数绑定 */
- template<typename F, typename... Args>
- std::function<void(void)> bindTask(F&& f, Args&&... args)
- {
- return std::bind(std::forward<F>(f), std::forward<Args>(args)...);
- }
- signals:
- void signal_startThread();
- void signal_runTask(std::function<void(void)> func);
- private slots:
- void do_timeoutWork();
- void do_runTask(std::function<void(void)> func);
- private:
- QTimer m_timerStartThread; /* 开启线程的定时器 */
- QThread* m_thread = nullptr;
- std::function<void(void)> m_func; /* 运行的函数 */
- };
- #endif // ONETHREAD_H
|