[webkit-qt] problem when using QtWebKit to get elements from page
qichao zhu
zqc.zhuqichao at gmail.com
Mon Sep 27 19:32:09 PDT 2010
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-qt/attachments/20100928/74f19aad/attachment.html>
More information about the webkit-qt
mailing list