123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- /**
- * 叶海辉
- * QQ群121376426
- * http://blog.yundiantech.com/
- */
- #include "FunctionTransfer.h"
- #include <QThread>
- #include <QDebug>
- Qt::HANDLE FunctionTransfer::gMainThreadId = nullptr;
- FunctionTransfer *FunctionTransfer::main_thread_forward = nullptr;
- Q_DECLARE_METATYPE(std::function<void()>)
- FunctionTransfer::FunctionTransfer(QObject *parent) :
- QObject(parent)
- {
- //因为std::function<void()>是自定义的类型 要跨线程传递需要先注册一下
- qRegisterMetaType<std::function<void()>>();
- connect(this, SIGNAL(comming(std::function<void()>)), this, SLOT(slotExec(std::function<void()>)), Qt::BlockingQueuedConnection);
- connect(this, SIGNAL(comming_noBlock(std::function<void()>)), this, SLOT(slotExec(std::function<void()>)), Qt::QueuedConnection);
- }
- FunctionTransfer::~FunctionTransfer()
- {
- }
- void FunctionTransfer::init()
- {
- FunctionTransfer::init(QThread::currentThreadId());
- }
- void FunctionTransfer::init(Qt::HANDLE id)
- {
- gMainThreadId = id;
- FunctionTransfer::main_thread_forward = new FunctionTransfer();
- }
- bool FunctionTransfer::isMainThread()
- {
- if (gMainThreadId == nullptr)
- {
- qDebug()<<__FILE__<<__LINE__<<__FUNCTION__<<"the main thread id is not set!";
- return false;
- }
- if (QThread::currentThreadId() == gMainThreadId)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- void FunctionTransfer::runInMainThread(std::function<void()> f, bool isBlock)
- {
- // FunctionTransfer::main_thread_forward->exec(f, isBlock);
- if(FunctionTransfer::isMainThread())
- {
- f();
- }
- else
- {
- if (isBlock)
- {
- Q_EMIT FunctionTransfer::main_thread_forward->comming(f);
- }
- else
- {
- Q_EMIT FunctionTransfer::main_thread_forward->comming_noBlock(f);
- }
- }
- }
- void FunctionTransfer::slotExec(std::function<void()> f)
- {
- f();
- // if(FunctionTransfer::isMainThread())
- // {
- // f();
- // }
- // else
- // {
- // if (isBlock)
- // {
- // Q_EMIT this->comming(f);
- // }
- // else
- // {
- // Q_EMIT this->comming_noBlock(f);
- // }
- // }
- }
|