Qt ------ Thread Affinity (QObject::moveToThread: Cannot move objects with a parent)
简单的说,每个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.
Qt ------ Thread Affinity (QObject::moveToThread: Cannot move objects with a parent)的更多相关文章
- 【QT】子类化QObject+moveToThread实现多线程
往期链接: <QThread源码浅析> <子类化QThread实现多线程> 从往期<QThread源码浅析>可知,在Qt4.4之前,run 是纯虚函数,必须子类化Q ...
- Qt StyleSheet皮肤(黑色,比较好看,而且很全)
使用方式如下 //设置皮肤样式 static void SetStyle(const QString &styleName) { QFile file(QString(":/imag ...
- Qt使用一个事件队列对所有发出的事件进行维护(QObject的event()函数相当于dispatch函数),用EventLabel 继承QLabel作为例子(简单明了) good
事件(event)是由系统或者 Qt 本身在不同的时刻发出的.当用户按下鼠标.敲下键盘,或者是窗口需要重新绘制的时候,都会发出一个相应的事件.一些事件在对用户操作做出响应时发出,如键盘事件等:另一些事 ...
- Qt程序打包(使用Enigma Virtual Box和BoxedApp Packer封包)
一.使用单文件封包工具 单文件封包工具,顾名思义就是将可执行文件及其相关依赖打包成单个可执行文件的工具. 这里推荐两个单文件封包工具:Enigma Virtual Box和BoxedApp Packe ...
- Qt之QFileIconProvider(根据扩展名获取文件图标、类型)
简述 在Qt之QFileIconProvider一节中已经讲解关于如何获取文件图标与类型.但只仍针对本地已存在的文件,此节,我们主要运用前面分享的内容,讲述如何通过任意后缀或本地不存在的文件来获取相关 ...
- Qt 线程基础(Thread Basics的翻译,线程的五种使用情况)
Qt 线程基础(QThread.QtConcurrent等) 转载自:http://blog.csdn.net/dbzhang800/article/details/6554104 昨晚看Qt的Man ...
- 重点:怎样正确的使用QThread类(注:包括推荐使用QThread线程的新方法QObject::moveToThread)
背景描述: 以前,继承 QThread 重新实现 run() 函数是使用 QThread唯一推荐的使用方法.这是相当直观和易于使用的.但是在工作线程中使用槽机制和Qt事件循环时,一些用户使用错了.Qt ...
- QT定时器的两种应用(QObject就有timerEvent事件,一种什么样的居心呢?)
QT中定时器的使用方法(1)重载timerEvent(QTimerEvent *)函数,然后再在类的构造函数中设置时间间隔 startTimer(50);//单位为毫秒(2)在类的构造函数中设定如 ...
- QThread 与 QObject的关系(QObject可以用于多线程,可以发送信号调用存在于其他线程的slot函数,但GUI类不可重入)
QThread 继承 QObject..它可以发送started和finished信号,也提供了一些slot函数. QObject.可以用于多线程,可以发送信号调用存在于其他线程的slot函数,也可以 ...
随机推荐
- Tensorflow框架之AlexNet
from datetime import datetime import math import time import tensorflow as tf batch_size=32 num_batc ...
- 利用Tensorflow进行自然语言处理(NLP)系列之二高级Word2Vec
本篇也同步笔者另一博客上(https://blog.csdn.net/qq_37608890/article/details/81530542) 一.概述 在上一篇中,我们介绍了Word2Vec即词向 ...
- ViewPort <meta>标记
ViewPort <meta>标记用于指定用户是否可以缩放Web页面,如果可以,那么缩放到的最大和最小缩放比例是什么.使用ViewPort <meta>标记还表示文档针对移动设 ...
- 什么是Meta标签? 哪些Meta标签对搜索引擎SEO优化有作用?
什么是Meta标签? Meta标签给搜索引擎提供了许多关于网页的信息,这些信息都是隐含信息,意味着对于网页自身的访问者是不可见的. 你可以在网页的 <head>元素中发现<meta& ...
- java 数据库查询Date类型字段 没有了时分秒 全为 00 的解决办法
当取出这个值的时候只能用java.sql.Date而且只能显示年月日,我想知道如何才能显示时分秒 PS:不改变用getdate()存入的前提下! 解决方法:将getDate()方法改为getTim ...
- 已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数
#include<iostream> using namespace std; //#define maxn 2000010 #include<stdio.h> //int a ...
- lintcode-418-整数转罗马数字
418-整数转罗马数字 给定一个整数,将其转换成罗马数字. 返回的结果要求在1-3999的范围内. 说明 什么是 罗马数字? https://en.wikipedia.org/wiki/Roman_n ...
- Hibernate(九)
三套查询之SQL查询 Native Sql Query原生的sql查询.要求写sql语句.SQLQuery 是 Query的子类 1.查询所有的学生 //1.查询所有的学生 @Test public ...
- Laravel中如何添加新字段,如何指定在某个字段后而不是添加在最后
解答:
- 【前端学习笔记】JavaScript 常用方法兼容性封装
获取样式函数封装 function getStyle(ele,attr){ if(ele.currentStyle){ return ele.currentStyle[attr]; } else{ r ...