此函数可以用来在子线程中委托主线程执行特定函数。QMetaObject::invokeMethod默认在主线程中执行函数,除非指定连接方式为Qt::DirectConnection。以下是测试用的代码和输出结果。头文件:
class MCcrt : public QThread { Q_OBJECT public: MCcrt(QObject* parent = 0) : QThread(parent) {} ~MCcrt() override = default; private: void run() override; private slots: void func(int a); };
CPP文件:
void MCcrt::run() { while (1) { qDebug() << u8"子线程:" << QThread::currentThread(); QMetaObject::invokeMethod(this, "func", Qt::AutoConnection, Q_ARG(int, 1)); QMetaObject::invokeMethod(this, "func", Qt::DirectConnection, Q_ARG(int, 2)); QMetaObject::invokeMethod(this, "func", Qt::QueuedConnection, Q_ARG(int, 3)); QMetaObject::invokeMethod(this, "func", Qt::BlockingQueuedConnection, Q_ARG(int, 4)); msleep(10000); } } void MCcrt::func(int a) { qDebug() << u8"我的线程:" << a << QThread::currentThread(); } <主窗口构造函数>() { ui.setupUi(this); qDebug() << u8"主线程:" << QThread::currentThread(); MCcrt* obj = new MCcrt; obj->start(); }
输出文本为:
主线程: QThread(0x20a8f310140) 子线程: MCcrt(0x20a8f327390) 我的线程: 2 MCcrt(0x20a8f327390) 我的线程: 1 QThread(0x20a8f310140) 我的线程: 3 QThread(0x20a8f310140) 我的线程: 4 QThread(0x20a8f310140) 子线程: MCcrt(0x20a8f327390) 我的线程: 2 MCcrt(0x20a8f327390) 我的线程: 1 QThread(0x20a8f310140) 我的线程: 3 QThread(0x20a8f310140) 我的线程: 4 QThread(0x20a8f310140)
标签:0x20a8f327390,0x20a8f310140,void,MCcrt,线程,测试,invokeMethod,QMetaObject,QThread From: https://www.cnblogs.com/mengxiangdu/p/17306199.html