QT网络模块QEventLoop

在HTTP通信中,使用QEventLoop进行阻塞等待时,通过`eventLoop.exec(QEventLoop::ExcludeUserInputEvents)`可能会导致用户输入事件(如滑块处理)堆积,从而在滑动停止后继续处理未完成的事件,造成卡顿现象。与之相比,`eventLoop.exec()`处理所有事件,包括用户输入,避免了这种情况。问题在于排除用户输入事件的处理可能导致事件队列积压。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:http通信中,采用code1阻塞通信,等待通信返回数据,发现在一些密集型耗时操作(滑块处理事件) 处理不及时,导致滑动停止后还在处理,但是采用code2并不这样

// code 1   
 
    QEventLoop eventLoop;
    connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec(QEventLoop::ExcludeUserInputEvents);

// code 2

    QEventLoop eventLoop;
    connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec();

两者的区别在在于:

  eventLoop.exec(QEventLoop::ExcludeUserInputEvents);

eventLoop.exec();//默认值QEventLoop::AllEvents

参数含义如下:


QEventLoop::AllEvents 0x00
All events. Note that DeferredDelete events are processed specially. See QObject::deleteLater() for more details.

QEventLoop::ExcludeUserInputEvents 0x01
Do not process user input events, such as ButtonPress and KeyPress. Note that the events are not discarded; they will be delivered the next time processEvents() is called without the ExcludeUserInputEvents flag.

QEventLoop::ExcludeSocketNotifiers 0x02
Do not process socket notifier events. Note that the events are not discarded; they will be delivered the next time processEvents() is called without the ExcludeSocketNotifiers flag.

QEventLoop::WaitForMoreEvents 0x04
Wait for events if no pending events are available.

QEventLoop::AllEvents 0x00
所有事件。 请注意,DeferredDelete 事件经过特殊处理。 有关更多详细信息,请参阅 QObject::deleteLater()。

QEventLoop::ExcludeUserInputEvents 0x01
不处理用户输入事件,例如 ButtonPress 和 KeyPress。 请注意,事件不会被丢弃; 它们将在下次调用 processEvents() 时不带 ExcludeUserInputEvents 标志时传递。

QEventLoop::ExcludeSocketNotifiers 0x02
不处理套接字通知程序事件。 请注意,事件不会被丢弃; 它们将在下次调用 processEvents() 而不带 ExcludeSocketNotifiers 标志时传送。

QEventLoop::WaitForMoreEvents 0x04
如果没有挂起的事件可用,则等待事件。

原因可能是code1不处理用户事件,但事件并不会被丢弃,导致事件堆积,所以滑动滑块停止后,还有很多事件没有处理完,导致还在处理,所以表现出很卡顿的现象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值