qt等待阻塞的解决方案

 

 

在开发中我们经常用到等待,阻塞,比如每个5秒钟请求一次服务器,但是这个时候我们使用QThread::sleep这个函数,但是这个函数有一个缺点,就是他会阻塞Qt的事件循环,我们界面会卡死。

 

所以有一种比较好的方法,就是QEventLoop。

 

				QTimer timer;
				timer.setInterval(5000);  // 设置超时时间 5 秒
				while (true)
				{
					QEventLoop eventLoop;
					connect(&timer, &QTimer::timeout, &eventLoop, &QEventLoop::quit);
					timer.start();
					eventLoop.exec();
					QByteArray bytes = HttpUtil::getInstance()->getPPTFileInfo(QUrl(urlString)); //获取ppt信息
                    if(bytes.isEmpty())
                    {
                        timer.stop();
                    }
                }

 

你可能感兴趣的:(Qt,客户端,C++)