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()函數中實例化並操作,這樣才屬於子線程本身。