Results 1 to 2 of 2

Thread: How to parse html with QWebElement?

  1. #1
    Join Date
    Dec 2014
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default How to parse html with QWebElement?

    Qt Code:
    1. #include <QApplication>
    2. #include <QDebug>
    3. #include <QWebPage>
    4. #include <QWebFrame>
    5. #include <QWebElement>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication a(argc, argv);
    10. QWebPage frame;
    11. frame.mainFrame()->setHtml("<html><body><p>First Paragraph</p><p>Second Paragraph</p></body></html>");
    12. QWebElement doc = frame.mainFrame()->documentElement();
    13. QWebElement body = doc.firstChild();
    14. qDebug() << body.toPlainText();
    15. QWebElement firstParagraph = body.firstChild();
    16. qDebug() << firstParagraph.toPlainText();
    17. QWebElement secondParagraph = firstParagraph.nextSibling();
    18. qDebug() << secondParagraph.toPlainText();
    19. return a.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 
    Output is :

    This example is from oficial doc so why this always empty and when i use other exaples it is empty too.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to parse html with QWebElement?

    Print the tagName() of each element and you will likely find that WebKit has provided the missing <head> element and that your "body" variable is pointing there. It has no content, so you see no output. That is what happened with Qt5.4 for me. Try:
    Qt Code:
    1. QWebElement body = doc.findFirst("body");
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. how to draw on a QWebElement
    By 26_zhang@163.com in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2014, 02:25
  2. Set html <style> with QWebElement
    By poser in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2014, 17:31
  3. QWebElement evaluateJavaScript problem
    By Talei in forum Newbie
    Replies: 0
    Last Post: 10th March 2011, 04:38
  4. Parse RSS into html and display it in QWebView.
    By halvors in forum Qt Programming
    Replies: 11
    Last Post: 14th August 2010, 12:44
  5. Best way to load and parse an HTML file ??
    By tuthmosis in forum Qt Programming
    Replies: 8
    Last Post: 23rd August 2008, 11:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.