Note that there are some explanatory texts on larger screens.

plurals
  1. POReceiving multiple loadFinished signals for a requested web page
    primarykey
    data
    text
    <p>I'm receiving multiple <code>loadFinished</code> signals when I attempt to load a <code>QWebPage</code> and I'm not sure what's causing the issue. There were a couple of other questions that seemed to allude to the same problem, but the solutions didn't work for me:</p> <ul> <li><a href="https://stackoverflow.com/questions/9966433/qtwebpage-loadfinished-called-multiple-times">QtWebPage - loadFinished() called multiple times</a></li> <li><a href="https://stackoverflow.com/questions/8415289/signal-qwebpageloadfinishedbool-returns-twice">Signal QWebPage::loadFinished(bool) returns twice?</a> </li> </ul> <p>In the first question, the answer was to connect signals to slots only once," but I already do that. The answer to the second question suggests that I should connect to the frame's <code>loadFinished</code> signal, but I simply don't get the necessary data when that is done.</p> <p>I attempt to load multiple pages:</p> <pre><code>int main(int argc, char *argv[]) { QApplication app(argc, argv); QList&lt;QUrl&gt; urls; urls.append(QUrl("http://www.useragentstring.com/pages/Chrome/")); urls.append(QUrl("http://www.useragentstring.com/pages/Firefox/")); urls.append(QUrl("http://www.useragentstring.com/pages/Opera/")); urls.append(QUrl("http://www.useragentstring.com/pages/Internet Explorer/")); urls.append(QUrl("http://www.useragentstring.com/pages/Safari/")); foreach(QUrl url, urls) { UA* ua = new UA(); QWebPage* page = new QWebPage(); //QObject::connect(page, SIGNAL(loadFinished(bool)), ua, SLOT(pageLoadFinished(bool))); QObject::connect(page-&gt;mainFrame(), SIGNAL(loadFinished(bool)), ua, SLOT(frameLoadFinished(bool))); // Load the page page-&gt;mainFrame()-&gt;load(url); } return app.exec(); } </code></pre> <p>The class that processes the signals looks like this:</p> <pre><code>class UA:public QObject { Q_OBJECT private: int _numPageLoadSignals; int _numFrameLoadSignals public: UA() { _numPageLoadSignals = 0; _numFrameLoadSignals = 0; } ~UA(){} public slots: void pageLoadFinished(bool ok) { _numPageLoadSignals++; QWebPage * page = qobject_cast&lt;QWebPage *&gt;(sender()); if(ok &amp;&amp; page) { qDebug() &lt;&lt; _numPageLoadSignals &lt;&lt; " loads " &lt;&lt; page-&gt;mainFrame()-&gt;documentElement().findAll("div#liste ul li a").count() &lt;&lt; " elements found on: " &lt;&lt; page-&gt;mainFrame()-&gt;requestedUrl().toString(); } } void frameLoadFinished(bool ok) { _numFrameLoadSignals++; QWebFrame * frame = qobject_cast&lt;QWebFrame *&gt;(sender()); if(ok &amp;&amp; frame) { qDebug() &lt;&lt; _numFrameLoadSignals &lt;&lt; " loads " &lt;&lt; frame-&gt;documentElement().findAll("div#liste ul li a").count() &lt;&lt; " elements found on: " &lt;&lt; frame-&gt;requestedUrl().toString(); } } }; </code></pre> <p>Here is the result of only connecting to the frame's <code>loadFinished</code> signal:</p> <pre><code>1 loads 0 elements found on: "http://www.useragentstring.com/pages/Safari/" 1 loads 0 elements found on: "http://www.useragentstring.com/pages/Chrome/" 1 loads 0 elements found on: "http://www.useragentstring.com/pages/Opera/" 1 loads 0 elements found on: "http://www.useragentstring.com/pages/Firefox/" 1 loads 241 elements found on: "http://www.useragentstring.com/pages/Internet Explorer/" </code></pre> <p>Here are the results when I connect to the page's <code>loadFinished</code> signal:</p> <pre><code>1 loads 0 elements found on: "http://www.useragentstring.com/pages/Safari/" 1 loads 0 elements found on: "http://www.useragentstring.com/pages/Chrome/" 1 loads 0 elements found on: "http://www.useragentstring.com/pages/Firefox/" 1 loads 0 elements found on: "http://www.useragentstring.com/pages/Internet Explorer/" 2 loads 576 elements found on: "http://www.useragentstring.com/pages/Safari/" 2 loads 782 elements found on: "http://www.useragentstring.com/pages/Chrome/" 2 loads 241 elements found on: "http://www.useragentstring.com/pages/Internet Explorer/" 2 loads 1946 elements found on: "http://www.useragentstring.com/pages/Firefox/" 3 loads 241 elements found on: "http://www.useragentstring.com/pages/Internet Explorer/" 3 loads 1946 elements found on: "http://www.useragentstring.com/pages/Firefox/" 3 loads 782 elements found on: "http://www.useragentstring.com/pages/Chrome/" 1 loads 964 elements found on: "http://www.useragentstring.com/pages/Opera/" 3 loads 576 elements found on: "http://www.useragentstring.com/pages/Safari/" </code></pre> <p>I don't understand the behavior, why sometimes I get relevant content and other times I don't. If I connect to the page's <code>loadFinished</code> signal, then I will eventually get the content but I don't know when it will actually happen. <strong>How do I know when my page has actually finished loading?</strong></p> <h2>Update</h2> <p>I'm assuming that most of my content will arrive in less than 3 seconds, so I've come up with a workaround: I set a timer event to signal the <code>UA::loadFinished</code> 3 seconds after the first <code>loadFinished</code> signal is received from the <code>QWebPage</code>. That's not very pretty, nor is it efficient, but it works for this situation.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload