1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #include "OneThread.h"
- #include <QDebug>
- OneThread::OneThread(QThread* thread, QObject* parent) : QObject(parent), m_thread(thread)
- {
- m_timerStartThread.setTimerType(Qt::PreciseTimer);
- m_timerStartThread.setSingleShot(true);
-
- this->moveToThread(m_thread);
-
-
- connect(&m_timerStartThread, &QTimer::timeout, this, &OneThread::do_timeoutWork);
- connect(this, &OneThread::signal_startThread, this, &OneThread::do_timeoutWork);
-
- connect(this, &OneThread::signal_runTask, this, &OneThread::do_runTask,Qt::QueuedConnection);
-
- qRegisterMetaType<std::function<int(void)>>("std::function<int(void)>");
- qRegisterMetaType<std::function<void(void)> >("std::function<void(void)>");
- }
- OneThread::~OneThread()
- {
- if(m_thread != nullptr)
- {
- m_thread->quit();
- m_thread->wait();
-
- m_thread = nullptr;
- }
- }
- void OneThread::startThread()
- {
- m_timerStartThread.start(0);
- }
- void OneThread::do_timeoutWork()
- {
- qDebug() << "----- do_timeoutWork , Thread ID: " << QThread::currentThreadId();
-
- qDebug() << "----- do_timeoutWork end -----";
- }
- void OneThread::do_runTask(std::function<void(void)> func)
- {
- qDebug() << "----- do_runTask , Thread ID: " << QThread::currentThreadId();
- func();
- qDebug() << "----- do_runTask end -----";
- }
|