[webkit-help] How to get the layout infomation use qtwebkit?

Umesh Singh sumeshfirst at gmail.com
Wed Feb 23 03:25:08 PST 2011


Hello Zhoupeng,
                         first you need to traverse the DOM and get all the
nodes then for each node you can get the
type and style using the renderStyleObject . Here's how u can do it.

Frame* frame = m_pPage->focusController()->focusedOrMainFrame(); // m_pPage
is of type Page
Document* focusedDocument = frame->document();
 frame = frame->tree()->top();
Node* focusedNode = focusedDocument->focusedNode();

    for (Frame* CurrentFrame = frame; CurrentFrame; CurrentFrame =
CurrentFrame->tree()->traverseNext(frame))
    {
        for (Node* candidate = pDocument->firstChild(); candidate; candidate
= candidate->traverseNextNode())
 {
// pDocument == CurrentFrame->document();
                // you will get all the nodes here for each node get its
renderer
               // RenderObject* pDestRender = candidate->renderer();
}
    }

2011/2/23 achellies <achellies at 163.com>

> Document* doc = frame()->document();
> if (doc && doc->firstChild())
>
> 我没有用qt的版本,上述代码能够获取到所有的元素
>
> 在 2011-02-23,"周鹏" <zhoupeng19 at 126.com> 写道:
>
> webkit-qt,你好!
>
>     Hi guys!
>     I want to get the page's layout infomation.eg:if there is a image in
> the page,i want to know the coordinates of  the image in the page,the
> background color, for text,i want to know it's font,size,and it's
> coordinates...
> here is my code:
> //traversal a dom tree
> void traversal(QWebElement node)
> {
>     ...
>  QRect rect= node.geometry();
>  printf("Start: X-%d,Y-%d  End: X-%d,Y-%d\n", ss.topLeft().x(),
> ss.topLeft().y(), ss.bottomRight().x(), ss.bottomRight().y() );
>     ...
> }
> int main(int argc, char *argv[])
> {
>     ...
>     QString content(htmlSource);
>     QUrl url("file:///home/rmss/zp/");
>     QApplication a(argc, argv);
>     QWebView *view = new QWebView;
>
>     QWebSettings * settings = QWebSettings::globalSettings();
>     //settings->setAttribute(QWebSettings::AutoLoadImages, false);
>     settings->setAttribute(QWebSettings::JavascriptEnabled, false);
>     settings->setAttribute(QWebSettings::JavaEnabled, false);
>     settings->setAttribute(QWebSettings::PluginsEnabled, false);
>     settings->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
>     settings->setDefaultTextEncoding("GB2312");
>
>     view->setHtml(content, url);
>     view->show();
>
>     QWebPage* page = view->page();
>     QWebFrame* frame = page->mainFrame();
>
>     QString str = frame->renderTreeDump();
>     QWebElement document = frame->documentElement();
>     QWebElement body = document.findFirst("body");
>     if(body .isNull())
>     {
>         printf("failed!\n");
>         return -1;
>     }
>     QString info = eee.styleProperty("background-color",
> QWebElement::ComputedStyle);//get background color
>     traversal(body );
>     return 0;
> }
>
> I use "geometry" to get the location of every node,but it's not work
> correctly,and this program can't process page which use external css. And
> please tell me how to get the font,size of text.I have not much time to read
> the qtwebkit code because my boss want me to fininsh this job in two days.
> So. please tell me how to solove these problem,and if possible,give me some
> demo.
>
> Apologize for my poor english!
> 周鹏
> zhoupeng19 at 126.com
> 2011-02-23
>
>
>
>
> _______________________________________________
> webkit-help mailing list
> webkit-help at lists.webkit.org
> http://lists.webkit.org/mailman/listinfo.cgi/webkit-help
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-help/attachments/20110223/90b17189/attachment-0001.html>


More information about the webkit-help mailing list