Results 1 to 8 of 8

Thread: Qt way of getting/parsing style attributes from html tags? local file batch

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt way of getting/parsing style attributes from html tags? local file batch

    This will get you everything. That may not be what you want. I'm not sure of a way of getting only author specified styles.
    Qt Code:
    1. #include <QtWebKit>
    2. #include <QApplication>
    3. #include <QtCore>
    4. int main(int argc, char * argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. QWebPage page;
    9. QWebFrame * frame = page.mainFrame();
    10. frame->setHtml("<html><head><style>div { color:blue; }</style><body><div style=\"font-size:120%;\">Hello World!</div></body></html>");
    11. QWebElement document = frame->documentElement();
    12. QWebElementCollection elements = document.findAll("div");
    13. foreach (QWebElement element, elements)
    14. {
    15. QString style = element.evaluateJavaScript("getComputedStyle(this).cssText").toString();
    16. QStringList styles = style.split(';');
    17. foreach(QString pair, styles)
    18. {
    19. QStringList keyvalue = pair.split(':');
    20.  
    21. if (keyvalue.length() == 2)
    22. qDebug() << keyvalue.at(0).trimmed() << " = " << keyvalue.at(1).trimmed();
    23. }
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2010
    Posts
    36
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt way of getting/parsing style attributes from html tags? local file batch

    Thanks numbat. I will check it out when I get home. Seems like your doing everything I need.

    Another thing, is there a more elegant way of putting the file into a QString than the following?

    Qt Code:
    1. QFile file("local.html");
    2. QTextStream in(&file);
    3. QString completeHtml;
    4. while (!in.atEnd()) {
    5. completeHtml.append(in.readLine());
    6. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt way of getting/parsing style attributes from html tags? local file batch

    You can use readAll. Here's a way to just get the rules in the style sheets:
    Qt Code:
    1. #include <QtWebKit>
    2. #include <QApplication>
    3. #include <QtCore>
    4. int main(int argc, char * argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. QWebPage page;
    9. QWebFrame * frame = page.mainFrame();
    10. frame->setHtml("<html><head><style>div { color:blue; } span {font-weight: bold;}</style><body><div style=\"font-size:120%;\">Hello World!</div></body></html>");
    11. QVariant vt = frame->evaluateJavaScript(
    12. "out = []; "
    13. "for (j = 0; j < document.styleSheets.length; j++) { "
    14. "for (i = 0; i < document.styleSheets[j].cssRules.length; i++) { "
    15. "out[i] = document.styleSheets[j].cssRules[i].style.cssText; } } "
    16. "out = out;");
    17. foreach (QVariant v, vt.toList())
    18. {
    19. qDebug() << v.toString();
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jun 2010
    Posts
    36
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt way of getting/parsing style attributes from html tags? local file batch

    Thanks again.

    Seriously, Qt has all the nutrients and vitamins a young programmer needs to grow healthy.

Similar Threads

  1. QRegExp for extracting the string between two HTML tags...
    By tuthmosis in forum Qt Programming
    Replies: 3
    Last Post: 27th May 2010, 06:55
  2. Matching HTML tags
    By pucara_faa in forum Qt Programming
    Replies: 4
    Last Post: 22nd January 2010, 13:19
  3. Html tags in QTreeView
    By 1111 in forum Qt Programming
    Replies: 1
    Last Post: 13th March 2009, 01:41
  4. QFontMetrics and HTML tags
    By vonCZ in forum Newbie
    Replies: 1
    Last Post: 14th August 2008, 12:13
  5. Replies: 1
    Last Post: 17th March 2006, 08:01

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.