我想在事件循环中实际处理之前
过滤一些发送给我的工作人员QThread
的不需要的事件。事件过滤器何时实际过滤事件:在调用线程中还是在事件循环中?
我认为答案是“在事件循环中”,但我在 qt 文档中找不到该确切问题的答案。QCoreApplication::postEvent(...)
postEvent()
QThread
问问题
1514 次
2 回答
2
worker 和它的事件过滤器都需要在同一个线程中。事件由线程的事件循环拾取,并在传递给它们的接收器之前通过过滤器QObject
(当过滤器允许时)。这是一个演示此行为的示例:
#include <QtCore>
//a thread that can be destroyed at any time
//see http://stackoverflow.com/a/25230470
class SafeThread : public QThread{
using QThread::run;
public:
explicit SafeThread(QObject *parent = nullptr):QThread(parent){}
~SafeThread(){ quit(); wait(); }
};
struct MyEvent : public QEvent {
static const QEvent::Type myType = static_cast<QEvent::Type>(2000);
MyEvent():QEvent(myType){}
};
//worker QObject that handles MyEvent by printing a message
class Worker : public QObject {
public:
explicit Worker(QObject *parent = nullptr):QObject(parent){}
bool event(QEvent *event) {
if(event->type() == MyEvent::myType) {
qInfo() << "event in thread: " << QThread::currentThreadId();
return true;
}
return QObject::event(event);
}
};
class EventFilter : public QObject {
public:
explicit EventFilter(QObject *parent = nullptr):QObject(parent){}
bool eventFilter(QObject *watched, QEvent *event) {
if(event->type() == MyEvent::myType) {
qInfo() << "filter in thread: " << QThread::currentThreadId();
return false; //do not filter the event
}
return QObject::eventFilter(watched, event);
}
};
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
//create worker and thread
Worker worker;
EventFilter filter;
SafeThread thread;
filter.moveToThread(&thread);
worker.moveToThread(&thread);
worker.installEventFilter(&filter);
thread.start();
qInfo() << "calling postEvent from thread: " << QThread::currentThreadId();
QCoreApplication::postEvent(&worker, new MyEvent());
QTimer::singleShot(1000, &a, &QCoreApplication::quit);
return a.exec();
}
输出应类似于:
calling postEvent from thread: 0xAAAA
filter in thread: 0xBBBB
event in thread: 0xBBBB
于 2018-03-28T23:43:04.250 回答
2
过滤必须在事件传递时执行,因为过滤器期望目标对象存在并且具有可访问状态。这种存在和状态只能在事件被传递的过程中得到保证。使用 's 方法是不安全QObject
的,从其他线程中保存一些选择,因此通常在发布事件时,不可能安全地访问目标对象!. 只有当对象的生命周期得到保证,并且使用线程安全的方法来访问状态,并且以不会导致死锁的方式完成时,才有可能这样做。
于 2018-03-30T16:09:36.937 回答