QT开发中文网

QThread* m_tgTcpServerThread = new QThread(this); TGHandle* tghandle = new TGHandle(this); TcpServer* m_tgServer = new TcpServer(); m_tgServer->moveToThread(m_tgTcpServerThread); m_tgServer->setHandler(tghandle); m_tgTcpServerT...

根据提供的代码片段,TGHandle 类的实例化是在主线程中进行的。因为 this 是作为父对象传递给 QThread 的构造函数,而父对象通常位于创建它的线程中。

然而,你也可以在另一个线程中实例化 TGHandle 对象,并将其移到目标线程。只需确保在 moveToThread() 调用之前创建 TGHandle 对象即可。

以下是一个示例:

// 创建 TGHandle 实例
TGHandle* tghandle = new TGHandle();

// 创建 QThread 实例并移动 tgServer 到该线程
QThread* m_tgTcpServerThread = new QThread();
TcpServer* m_tgServer = new TcpServer();
m_tgServer->moveToThread(m_tgTcpServerThread);
m_tgServer->setHandler(tghandle);

// 启动新线程
m_tgTcpServerThread->start();

这样,tghandlem_tgServer 将在主线程和 m_tgTcpServerThread 线程之间共享连接,并且 tghandle 将位于 m_tgTcpServerThread 线程中执行其处理逻辑。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=12097

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?