Qt Socket多线程编程出现的问题

http://blog.sina.com.cn/s/blog_640531380102y5rl.html

  • 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)

(Parent is QTcpSocket(0x7a50f68), parent's thread is QThread(0x105d070), current thread is DevNetConnect(0x3ebfdf8)

如果在构造函数中实例化了QTcpSocket,则该对象将属于调用该子线程的对象而非子线程本身;

  • 解决:

不要在构造函数中实例化,而是在子线程的run()函数中实例化并操作,这样才属于子线程本身。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章