Qt之网络编程错误总结

25 篇文章 0 订阅


最近在做Qt网络编程,主要是项目需要,学习下Qt下网络编程的多线程实现。

 1。

QTcpSocket 中的connectToHost() 是void类型的,所以调用这个函数后无论是否连上就发送数据的话,在未连接的情况下会出现如下错误

QNativeSocketEngine::write() was not called in QAbstractSocket::ConnectedState

 

解决方法:通过connected() 信号设置bool变量值,根据bool值判断是否发送数据


 2。

 下面的是因为没有指定 connect的链接方向 Qt::DirectConnection

默认会发送给主线程即默认的连接Qt::AutoConnection ,如果是多线程的话就是Qt::QueuedConnection了,所以要明确指定

QObject::connect: Cannot queue arguments of type 'QAbstractSocket::SocketError'
(Make sure 'QAbstractSocket::SocketError' is registered using qRegisterMetaType

().)

 

 

This enum describes the types of connection that can be used between signals and slots. In particular, it determines whether a particular signal is delivered to a slot immediately or queued for delivery at a later time.

 

ConstantValueDescription
Qt::AutoConnection0(default) Same as DirectConnection, if the emitter and receiver are in the same thread. Same as QueuedConnection, if the emitter and receiver are in different threads.
Qt::DirectConnection1The slot is invoked immediately, when the signal is emitted.
Qt::QueuedConnection2The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.
Qt::BlockingQueuedConnection4Same as QueuedConnection, except the current thread blocks until the slot returns. This connection type should only be used where the emitter and receiver are in different threads. Note: Violating this rule can cause your application to deadlock.
Qt::UniqueConnection0x80Same as AutoConnection, but the connection is made only if it does not duplicate an existing connection. i.e., if the same signal is already connected to the same slot for the same pair of objects, then the connection will fail. This connection type was introduced in Qt 4.6.
Qt::AutoCompatConnection3The default type when Qt 3 support is enabled. Same as AutoConnection but will also cause warnings to be output in certain situations. See Compatibility Signals and Slots for further information.


3。

注册这个宏,我就不是很理解,请各位指教,谢谢,mark下

int type1=qRegisterMetaType<QAbstractSocket::SocketError>("SocketError");

 

4。

在多线程socket编程中 ,在每个run函数的末尾加上exec();不知道是什么原因,这个消息循环必须添加,不然程序不能正常执行,

我的猜测是让线程一直在这个位置循环吧,具体的我也讲不太清楚。请高手详解,谢谢

http://blog.csdn.net/vah101/article/details/5980070

http://www.qtcentre.org/threads/6168-QSocketNotifier-socket-notifiers-cannot-be-disabled-from-another-thread

  • 13
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钱国正

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值