简体   繁体   English

Qt moveToThread:对象带来了哪些资源?

[英]Qt moveToThread: What resources are brought with the object?

Say I have created a QObject a and it has a member QObject b . 假设我创建了一个QObject a ,它有一个成员QObject b (Actually, both A and B are subclasses of QObject, and class A has a member B b .) (实际上,A和B都是QObject的子类,而A类有成员B b 。)

When b is created, its parent is 0 (default). 创建b ,其父级为0(默认值)。 In the code, if I never set b 's parent to a , and if I call movetothread() to move a into a worker thread, will b be moved into that thread too ? 在代码中,如果我从来没有b的父母a ,如果我叫movetothread()移动a成工作线程, b移动到那个线程呢

If it is not moved, if I call b.init() from the worker thread (the one I moved a into) which use new operator to create another QObject that has b as a parent, then I will get the following error, right? 如果它不动,如果我叫b.init()从工作线程(一个我搬到a成),它使用new运算符来创建具有另一个QObject的b作为父母,那么我会得到下面的错误,正确的?

QObject: Cannot create children for a parent that is in a different thread QObject:无法为位于不同线程中的父级创建子级

As the Qt documentation for QObject::moveToThread states: - 正如QObject :: moveToThread的Qt文档所述: -

Changes the thread affinity for this object and its children. 更改此对象及其子对象的线程关联。 The object cannot be moved if it has a parent. 如果对象具有父对象,则无法移动该对象。 Event processing will continue in the targetThread. 事件处理将在targetThread中继续。

In this case, a parent is an object whose child is set either by passing the parent in the constructor or by calling setParent on the child. 在这种情况下,父对象是一个对象,其子对象是通过在构造函数中传递父对象或通过在子对象上调用setParent来设置的。 It is not an an object which has a pointer to another object. 它不是一个具有指向另一个对象的指针的对象。

In the code, if I never set b's parent to a, and if I call movetothread() to move a into a worker thread, will b be moved into that thread too? 在代码中,如果我从未将b的父级设置为a,并且如果我调用movetothread()将a移动到工作线程中,那么是否也会将其移动到该线程中?

So, no, if b's parent is not set and you call moveToThread on 'a', 'b' will still have the original thread affinity. 所以,不,如果没有设置b的父级,并且你在'a'上调用moveToThread,'b'仍将具有原始的线程关联。

If it is not moved, if I call b.init() from the worker thread... 如果没有移动,如果我从工作线程调用b.init()...

If you've moved 'a' and not 'b' to a worker thread, then you should not be calling b.init directly from the worker thread. 如果你将'a'而不是'b'移动到工作线程,那么你不应该直接从工作线程调用b.init。 Instead, the object in the worker thread ('a') should emit a signal for an object in the original thread to call b.init from a connected slot 相反,工作线程('a')中的对象应该为原始线程中的对象发出信号,以从连接的插槽调用b.init

will b be moved into that thread too? b移动到那个线程呢?

No, QObject doesn't know that b is a member of a . 不, QObject不知道, b是的成员a (You didn't say whether a holds a pointer to b or whether it holds b directly, but the answer is the same in both cases.) (你没有说是a是否持有指向b的指针或者它是否直接包含b ,但在两种情况下答案是相同的。)

then I will get the following error, right? 然后我会得到以下错误,对吧?

The child of a QObject must always be created in the thread which owns the parent. 必须始终在拥有父级的线程中创建QObject的子级。 I'm reluctant to say that you will definitely get an error, because the behaviour may be undefined. 我不愿意说你肯定会收到错误,因为行为可能是未定义的。

See Threads and QObject . 请参见Threads和QObject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM