SL100_FactoryTestTool/README.md

22 lines
1.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 产测工具
遇到问题:
2024.09.01
1. 运行报错QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
问题分析:主线程创建了 QTcpSocket这个QTcpSocket的读写操作必须在主线程中执行不能将QTcpSocket对象通过新建的对象传参后到新建对象中对QTcpSocket进行读写操作所有的QTcpSocket 的操作都在与它关联的线程中执行。
问题解决1. 在主线程中创建 QTcpSocket 并获取连接:
继续在主线程中使用 server->nextPendingConnection() 来获取客户端连接。
2. 将 QTcpSocket 移动到 ClientHandler 所属的新线程:
在将 QTcpSocket 传递给 ClientHandler 之前,将 QTcpSocket 移动到新线程中,确保它在新线程中被使用。
这里注意不能将有父对象的QTcpSocket 移动到新线程中,因为父对象负责管理子对象的生命周期,如果子对象被移动到其他线程,可能会导致线程安全问题或不一致的对象状态。
运行报错QObject::moveToThread: Cannot move objects with a parent
解决方法:如果 QTcpSocket 已经有父对象,你需要先将其父对象设置为 nullptr再移动到新的线程。
3. 在 ClientHandler 内对 QTcpSocket 进行操作:
在 ClientHandler 中,确保所有 QTcpSocket 的操作都在同一线程内进行。
mDNS实现原理