- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设一个对象 obj
属于QThread T1
.最好在 Qhread T2
的函数,obj
无法从 T1
中“拉出”至 T2
.这是在 moveToThread()
中提到的文档:
Warning: This function is not thread-safe; the current thread must be same as the current thread affinity. In other words, this function can only "push" an object from the current thread to another thread, it cannot "pull" an object from any arbitrary thread to the current thread. There is one exception to this rule however: objects with no thread affinity can be "pulled" to the current thread.
moveToThread(nullptr)
将使一个对象可以从其他线程移动。
void FunctionRunningInT2 (QObject& obj) // `obj` belongs to thread `T1`
{
obj.moveToThread(nullptr); // line-1 no event processing for obj!?
obj.moveToThread(T2); // line-2 is it OK ???
}
signal
在
obj
上发出第 1 行和第 2 行之间?
obj.disconnect()
,之后它不接受任何信号。然而,在
disconnect()
之前未决的信号还在处理中。
moveToThread(nullptr)
是真的吗?还有?或者它也会丢弃待处理的信号?
最佳答案
据我了解,就是不能打电话moveToThread
在不同线程中的对象上。即使参数是 nullptr
,您最终会收到来自 Qt 的以下消息:
QObject::moveToThread: Current thread ( ... ) is not the object's thread ( ... )
Cannot move to target thread (0x0)
class Moveable : public QObject
{
Q_OBJECT
public:
Moveable() : QObject(nullptr) {}
public slots:
void moveMe(QThread * destination) { moveToThread(destination); }
class Mover : public QObject
{
Q_OBJECT
signals:
void moveIt(QThread *);
connect(&mover, &Mover::moveIt, &moveable, &Moveable::moveMe, Qt::QueuedConnection);
mover.moveIt(QThread::currentThread());
mover.moveIt(to_whatever_thread);
QMetaObject::invokeMethod(&moveable, "moveMe", Qt::QueuedConnection, Q_ARG(QThread*, destination_thread));
0x0
,如前所述
here :
no event processing for this object or its children can happen, as they are no longer associated with any thread.
0x0
的对象接收信号。 .
关于c++ - `moveToThread(nullptr)` 是从源线程拉出目标线程内的 QObject 的有效方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59675298/
我是一名优秀的程序员,十分优秀!