QWebView等待加载 [英] QWebView Wait for load

查看:130
本文介绍了QWebView等待加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bool MainWindow::waitForLoad(QWebView& view)
{
    QEventLoop loopLoad;
    QTimer timer;
    QObject::connect(&view, SIGNAL(loadFinished(bool)), &loopLoad, SLOT(quit()));
    QObject::connect(&timer, SIGNAL(timeout()), &loopLoad, SLOT(quit()));
    timer.start(timeout);
    loopLoad.exec();
    if(!timer.isActive())
    {
        timer.stop();
        view.stop();
        return false;
    }
    return true;
}

说我,这是正确的代码吗?应用有时会在下线后冻结

Say me, is that a correct code? App sometimes freezes after line

loopLoad.exec();

即使在这里发生了一些问题(超时,加载时出错等),也总是返回true.

And always returns true even here has happened some problem (timeout, errors when loading, ect -- always true).

推荐答案

start(timeout);以毫秒为单位的超时间隔启动计时器.因此,在调用它之后,计时器开始运行,并且timer.isActive()始终返回true,并且if块未执行.

start(timeout); starts the timer with a timeout interval of msec milliseconds. So after calling it the timer is running and timer.isActive() always returns true and the if block does not get executed.

应该在发出loadFinished时停止计时器:

You should stop the timer when loadFinished is emitted :

QObject::connect(&view, SIGNAL(loadFinished(bool)), &timer, SLOT(stop()));

如果计时器处于活动状态,则计时器将停止事件循环,因此您应该返回false,因为发生了超时.您应该将if(!timer.isActive())替换为if(timer.isActive()).

If the timer is active then the event loop is stopped by the timer so you should return false because a timeout has been occurred. You should replace if(!timer.isActive()) with if(timer.isActive()).

正确的代码是:

bool MainWindow::waitForLoad(QWebView& view)
{
    QEventLoop loopLoad;
    QTimer timer;
    QObject::connect(&view, SIGNAL(loadFinished(bool)), &loopLoad, SLOT(quit()));
    QObject::connect(&view, SIGNAL(loadFinished(bool)), &timer, SLOT(stop()));
    QObject::connect(&timer, SIGNAL(timeout()), &loopLoad, SLOT(quit()));
    timer.start(timeout);
    loopLoad.exec();
    if(timer.isActive())
    {
        timer.stop();
        view.stop();
        return false;
    }

    return true;
}

这篇关于QWebView等待加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆