QTcpSocket多线程
(2018-04-28 18:08:27)| 分类: QT |
1. QTcpSocket不能跨线程访问
比如说我在子线程的构造函数对其成员QTcpSocket进行了实例化,然后在其run()函数中进行操作;
运行时一般会出现如下信息:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNativeSocketEngine(0xadc00df0), parent*s thread is QThread(0x81c0298), current thread is QThread(0x806e000)
如果在构造函数中实例化了QTcpSocket,则该对象将属于调用该子线程的对象而非子线程本身;
解决: 不要在构造函数中实例化,而是在子线程的run()函数中实例化并操作,这样才属于子线程本身。
比如说我在子线程的构造函数对其成员QTcpSocket进行了实例化,然后在其run()函数中进行操作;
运行时一般会出现如下信息:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNativeSocketEngine(0xadc00df0), parent*s thread is QThread(0x81c0298), current thread is QThread(0x806e000)
如果在构造函数中实例化了QTcpSocket,则该对象将属于调用该子线程的对象而非子线程本身;
解决: 不要在构造函数中实例化,而是在子线程的run()函数中实例化并操作,这样才属于子线程本身。
前一篇:拷贝构造函数和赋值运算符
后一篇:Linuxscp命令

加载中…