Qt在不同线程使用socket报错:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTcpServer(0x1538a748), parent’s thread is QThread(0x15380578), current thread is QThread(0x15388d20)
测试方法
1、创建一个新的类CSocketTest,继承QObject;
2、创建一个QThread的对象;
3、创建一个QTcpServer的对象;
4、在初始化函数中,调用new QTcpServer,然后在connect的槽函数中调用new QTcpServer(子线程);
测试代码
CSocketTest的头文件
包含头文件:
QThread
QTcpServer
定义变量:
private:
QThread m_threadTest;
QTcpServer *m_tcpServer;
定义初始化函数:
void init();
定义信号,槽:
signals:
void notifySocketTest();
public slots:
void onNotifySocketTest();
CSocketTest的cpp文件
构造函数,使用moveToThread关联类与