Hi Qichao. Let me try to explain the problem.

void View::loadFinished(bool ok)

{

    Q_UNUSED(ok);

    QWebFrame *frame = page()->mainFrame();

    QWebElement element = frame->findFirstElement("div");

    qDebug() << frame->toHtml();

    QWebView webView;

    webView.setUrl(QUrl("http://www.google.com";));

    qDebug() << webView.page()->mainFrame()->toHtml();

}



You have two QWebViews here. One that is the current "this" element (we'll call 
that one A) and one that you instantiate inline with the variable you call 
webView (we'll call that one B). You've reached this signal once (A) was 
loaded, and then you try to load a URL into (B). The difference is that you 
access the HTML of (A) after it's already loaded, but you access the HTML of 
(B) before it's fully loaded. You can't expect google.com to be loaded from the 
network immediately after you request for it, networks still take some time - 
and this is an asynchronous API. :)

If you want this to work, you should keep a live pointer to your webView (B), 
and connect its loadFinished signal to another slot, instead of calling 
webView.page()->mainFrame()->toHtml() synchronously after setting the URL.



If you have questions, please reply to the list and not to me directly, there 
might be others interested in your question :)
Regards
No'am

From: [email protected] 
[mailto:[email protected]] On Behalf Of ext qichao zhu
Sent: Monday, September 27, 2010 7:32 PM
To: [email protected]
Subject: [webkit-qt] problem when using QtWebKit to get elements from page

Hi all,
I am using Qt SDK by Nokia v2010.04 (open source). I want to get webelement 
from page through webkit component but the result confuses me.

If using code like this:

main.h



#ifndef MAIN_H

#define MAIN_H



#include <QtGui>

#include <QtWebKit>



class View : public QWebView

{

    Q_OBJECT



public:

    View(QWidget *parent = 0);



private slots:

    void loadFinished(bool ok);

};



#endif // MAIN_H



main.cpp

#include "main.h"



View::View(QWidget *parent) : QWebView(parent)

{

    setUrl(QUrl("http://www.google.com";));

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

}



void View::loadFinished(bool ok)

{

    Q_UNUSED(ok);

    QWebFrame *frame = page()->mainFrame();

    QWebElement element = frame->findFirstElement("div");

    qDebug() << element.toPlainText();

}



int main(int argc, char *argv[])

{

    QApplication app(argc, argv);



    View view;

    view.show();



    return app.exec();

}



The debug output will print out some html. But if I change main.cpp code as 
follow:









#include "main.h"





View::View(QWidget *parent) : QWebView(parent)





{



    setUrl(QUrl("http://www.google.com";));





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





}





void View::loadFinished(bool ok)





{



    Q_UNUSED(ok);





    QWebFrame *frame = page()->mainFrame();





    QWebElement element = frame->findFirstElement("div");





    qDebug() << frame->toHtml();







    QWebView webView;





    webView.setUrl(QUrl("http://www.google.com";));





    qDebug() << webView.page()->mainFrame()->toHtml();





}





int main(int argc, char *argv[])





{



    QApplication app(argc, argv);







    View view;



    view.show();





    return app.exec();





}



The second debug output is "<html><head></head><body></body></html>". Why can't 
I just create a simple QWebView instance for retrieving elements? I have 
created a post on forum Nokia for this issue 
(http://discussion.forum.nokia.com/forum/showthread.php?210271-webelement-can-t-be-retrieved-in-Simple-Selector-Example&p=779849#post779849).
 Thanks.






regards,

qichao





_______________________________________________
webkit-qt mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-qt

Reply via email to