Qt ------ Thread Affinity (QObject::moveToThread: Cannot move objects with a parent)
2017-11-03 15:13
3071 查看
简单的说,每个QObject的对象,都和某个创建对象所在的线程关联,如果把对象通过 moveToThread 移动到其他线程,这个对象不能有父对象,否则会出现 QObject::moveToThread: Cannot move objects with a parent
A QObject instance is said to have a thread affinity, or that it lives in a certain thread. When a QObject receives a queued signal or a posted event, the slot or event handler will run in the thread that the object lives in.
Note: If a QObject has no thread affinity (that is, if thread() returns zero), or if it lives in a thread that has no running event loop, then it cannot receive queued signals or posted events.
By default, a QObject lives in the thread in which it is created. An object's thread affinity can be queried using thread() and changed using moveToThread().
All QObjects must live in the same thread as their parent. Consequently:
setParent() will fail if the two QObjects involved live in different threads.
When a QObject is moved to another thread, all its children will be automatically moved too.
moveToThread() will fail if the QObject has a parent.
If QObjects are created within QThread::run(), they cannot become children of the QThread object because the QThread does not live in the thread that calls QThread::run().
Note: A QObject's member variables do not automatically become its children. The parent-child relationship must be set by either passing a pointer to the child's constructor, or by calling setParent(). Without this step, the object's member variables will remain in the old thread when moveToThread() is called.
A QObject instance is said to have a thread affinity, or that it lives in a certain thread. When a QObject receives a queued signal or a posted event, the slot or event handler will run in the thread that the object lives in.
Note: If a QObject has no thread affinity (that is, if thread() returns zero), or if it lives in a thread that has no running event loop, then it cannot receive queued signals or posted events.
By default, a QObject lives in the thread in which it is created. An object's thread affinity can be queried using thread() and changed using moveToThread().
All QObjects must live in the same thread as their parent. Consequently:
setParent() will fail if the two QObjects involved live in different threads.
When a QObject is moved to another thread, all its children will be automatically moved too.
moveToThread() will fail if the QObject has a parent.
If QObjects are created within QThread::run(), they cannot become children of the QThread object because the QThread does not live in the thread that calls QThread::run().
Note: A QObject's member variables do not automatically become its children. The parent-child relationship must be set by either passing a pointer to the child's constructor, or by calling setParent(). Without this step, the object's member variables will remain in the old thread when moveToThread() is called.
相关文章推荐
- qt thread with movetothread
- 【Qt】Qt出现QObject: Cannot create children for a parent that is in a different thread.问题
- Qt出现QObject: Cannot create children for a parent that is in a different thread.问题
- MovetoThread : Widgets cannot be moved to a new thread
- 一种使用QThread线程的新方法QObject::moveToThread
- Cannot send events to objects owned by a different thread
- QT线程出现错误Cannot create children for a parent that is in a different thread
- 在Qt使用moveToThread() qt的线程
- Qt moveToThread使用说明
- ASSERT “Cannot send events to objects owned by a different thread.” SoQt Coin Qt
- 在Qt使用moveToThread()
- Qt 多线程之MoveToThread()
- QObject::moveToThread() 打开相机
- 【Qt】QThread::moveToThread
- Qt里出现 Cannot create children for a parent that is in a different thread
- QThread with movetothread
- QObject: Cannot create children for a parent that is in a different thread.
- 一种使用QThread线程的新方法QObject::moveToThread
- QObject: Cannot create children for a parent that is in a different thread.
- Qt中次线程里创建对象,出现Cannot create children for a parent that is in a different thread