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