Results 1 to 8 of 8

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

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

    Thumbs up Qt way of getting/parsing style attributes from html tags? local file batch

    I have this old and dirty code I wrote some years ago that basically parsed css styles from local html documents. That code is really really ugly, and I would like to Qtify it!

    I don't need rendering the webpage at all, just opening local files, reading them and closing them (in batch). How would you guys tackle this?

    I first thought using the webkit somehing like:
    Qt Code:
    1. QWebPage page;
    2. QWebFrame * frame = page.mainFrame();
    3. QUrl fileUrl("localFile.html");
    4. frame->setUrl(fileUrl);
    5. QWebElement document = frame->documentElement();
    6. QWebElementCollection elements = document.findAll("div");
    7. foreach (QWebElement element, elements){
    8. std::cout << element.attribute("style").toStdString() << std::endl;
    9. }
    To copy to clipboard, switch view to plain text mode 
    That didn't work... Why?

    Can I work with html files like legal xml? In that case, getting the attributes from the styles is going to be somewhat annoying, knowing all the styles are declared in the "head"...

    Any ideas, what Qclasses can be helpful here?
    Last edited by toglia3d; 10th June 2010 at 16:24.

  2. #2
    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 

  3. #3
    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 

  4. #4
    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 

  5. #5
    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    You don't need QtWebKit just to parse an xml file. Either use one of QXmlStreamReader or QDomDocument or use QtXmlPatterns module to query for specific tags in the file. If you just want contents of all "style" attributes in the file, the last approach will be best. Something like this might work:
    Qt Code:
    1. QXmlQuery qry;
    2. qry.setQuery("doc('file.html')//@style"); // "doc('...')//div@style" will give you all 'style' attrs from all 'div' tags
    3. // or:
    4. // QFile f("file.html");
    5. // f.open(QFile::ReadOnly|QFile::Text);
    6. // qry.setFocus(&f);
    7. // qry.setQuery("//@style");
    8. qry.evaluateTo(&list);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    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

    Huh? Please excuse me while I pick my jaw up from the ground.
    Thats just beautiful.

  8. #8
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

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

    Thanks: Found this thread by searching, answered my question.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.