QtWebPage - loadFinished()多次调用 [英] QtWebPage - loadFinished() called multiple times

查看:955
本文介绍了QtWebPage - loadFinished()多次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有列表视图。选择其中的一个项目会触发一个事件:

In my application, I have list view. Selecting another item in it, triggers an event:

connect(listView->selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(item_changed(const QModelIndex &, const QModelIndex &)));

void MainWindow::item_changed(const QModelIndex & current, const QModelIndex & previous)
{
    qDebug() << "\n" << "item_changed(), caller: " << sender()->objectName();
    if (current.isValid())
    {
        /*
          not so important code
        */

        change_query(tokens.join("+"));
    }
}

这会调用另一个插槽 - change_query p>

This calls another slot - change_query().

void MainWindow::change_query(QString newquery)
{
    qDebug() << "change_query(), caller: " << sender()->objectName();

    QUrl query (newquery);

    frame->load(query);

    connect(frame, SIGNAL(loadFinished(bool)), this, SLOT(loading_finished()));
}



最后,当页面完全加载时,应调用load_finished / p>

And finally, when page is fully loaded, it should call loading_finished()

void MainWindow::loading_finished()
{
    qDebug() << "loading_finished(), caller: " << sender()->objectName();
}

但是,令我惊讶的是,输出是:

But, to my surprise, the output is:

item_changed(), caller:  "SelectionModel" 
change_query(), caller:  "SelectionModel"
loading_finished(), caller:  "frame" 

item_changed(), caller:  "SelectionModel" 
change_query(), caller:  "SelectionModel" 
loading_finished(), caller:  "frame" 
loading_finished(), caller:  "frame" 

item_changed(), caller:  "SelectionModel" 
change_query(), caller:  "SelectionModel" 
loading_finished(), caller:  "frame" 
loading_finished(), caller:  "frame" 
loading_finished(), caller:  "frame" 

item_changed(), caller:  "SelectionModel" 
change_query(), caller:  "SelectionModel" 
loading_finished(), caller:  "frame" 
loading_finished(), caller:  "frame" 
loading_finished(), caller:  "frame" 
loading_finished(), caller:  "frame" 

正如你所看到的,创建并加载WebFrame的另一个实例(?),或者每个循环加载页面+1次。我花了最后2个小时找出问题在哪里,我没有看到任何东西。

As you can see, each time i change selection, another instance (?) of WebFrame is created and loaded, or page is loaded +1 time each loop. I spent last 2 hours finding out where the problem is and I don't see anything.

推荐答案

相反,您可以调用

connect(frame, SIGNAL(loadFinished(bool)), this, SLOT(loading_finished()));

所以,你的插槽被调用这么多次,因为你调用 connect

evety time you change item. So, your slots gets called so many times, as you called connect.

这篇关于QtWebPage - loadFinished()多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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