How to Simply Parse a CSS Like (!) File in My Qt Application

How can I simply parse a CSS like (!) file in my Qt application?

I know two possibilities:

  1. boost::spirit and here you can find a good introduction to the boost::spirit parser framework
  2. I would recommend to write your own recursive descent parser

Due to the fact, that your personalized *.pss is not that complex as a CSS (simple bracketing etc.), I would recommend 2.

How to access the set qt stylesheet properties (css like grammar), or is there an css to xml converter?

I think you will need to use private Qt classes to do this. This is generally not a good idea as the interfaces are internal and subject to change.

In the Qt (4.8.4) sources \src\gui\text\qcssparser_p.h header the QCss namespace is declared.

Whilst I haven't tried this, it looks like you will need to create a QCss::Parser, call parse to get a QCss::StyleSheet. This object contains the parsed data including a vector of QCss::StyleRule which matches QCss::Selector and QCss::Declaration together, have a look at the comment above the QCss::Declaration to see how it is all broken down.

Final Warning: Using Qt private interfaces is liable to cause maintenance problems - don't do it without a very good reason.

Editing QSS Stylesheet programmatically

There is a terrible asymmetry between the dynamic C++ setters and getters for several (but far from all) properties that can be set via QSS.

To make the problem worse, as you noticed, there is no decent way to edit the active stylesheet other than refreshing the whole thing, which means reloading everything. To top it all off, there is no easy programmatic way to actually edit the stylesheet once loaded. It's a structured string, and for parsing and modifying it, you need to resort to boilerplate code, written by you.

Not using stylesheets isn't an option either, as various properties cannot be set without them.

How to use structured QDataStream and Serialization?

Here is a very nice example. Your propertyID could be structured in a seperate object like the UserRecord in this example..


  1. You would have to go all preceeding 'Records'

How to parse an HTML file with QT?

I recommend to use htmlcxx. It is licensed under LPGL. It works on Linux and Windows. If you use windows compile with msys.

To compile it just extract the files and run

./configure --prefix=/usr/local/htmlcxx
make
make install

In your .pro file add the include and library directory.

INCLUDEPATH += /usr/local/htmlcxx/include
LIBS += -L/usr/local/htmlcxx/lib -lhtmlcxx

Usage example

#include <iostream>
#include "htmlcxx/html/ParserDom.h"
#include <stdlib.h>

int main (int argc, char *argv[])
{
using namespace std;
using namespace htmlcxx;

//Parse some html code
string html = "<html><body>hey<A href=\"www.bbxyard.com\">myhome</A></body></html>";
HTML::ParserDom parser;
tree<HTML::Node> dom = parser.parseTree(html);
//Print whole DOM tree
cout << dom << endl;

//Dump all links in the tree
tree<HTML::Node>::iterator it = dom.begin();
tree<HTML::Node>::iterator end = dom.end();
for (; it != end; ++it)
{
if (strcasecmp(it->tagName().c_str(), "A") == 0)
{
it->parseAttributes();
cout << it->attribute("href").second << endl;
}
}

//Dump all text of the document
it = dom.begin();
end = dom.end();
for (; it != end; ++it)
{
if ((!it->isTag()) && (!it->isComment()))
{
cout << it->text() << " ";
}
}
cout << endl;
return 0;
}

Credits for the example:
https://github.com/bbxyard/sdk/blob/master/examples/htmlcxx/htmlcxx-demo.cpp

You can't use an XML parser for HTML. You either use htmlcxx or convert the HTML to valid XML. Then you are free to use QDomDocument, Qt XML parsers, etc.

QWebEngine has also parsing functionality, but brings a large overhead with the application.



Related Topics



Leave a reply



Submit