views:

145

answers:

1

I am new to Qt.

I am building a console application and I need to process lot of real world html pages. QtWebkit comes as an easy choice because of clearly cut APIs and easy availability.

I checked out the docs and they say that I can load pages by using QWebView::load(). But I am building a console application and I cannot use a widget. I get the error as: ?

QWidget: Cannot create a QWidget when no GUI is being used
The program has unexpectedly finished.

So how can I process the html pages using QtWebkit in console application.

+5  A: 

QWebPage can be used in a widget-less environment.

To load a page, do something like this

QWebPage page;
QUrl url = ...;
page.mainFrame()->load(url);

To get access to the DOM tree, you can use QWebFrame::documentElement(). See the API for how to use this.

Job
How do I load pages (actually from my local storage) using it?
Xolve
@Xolve: See my edits.
Job
@Job thanks. Actually I just need to parse the dom tree.
Xolve
@Xolve: Edited my answer to show how to get access to the DOM tree.
Job
If you like the answer you can accept it.
Ross
@Job I tried it and it doesn't work. Gives me the same error that GUI is not present.
Xolve
http://pastebin.com/aC8mbDTU
Xolve
@Xolve: Hmm that's very strange... It should work according to the [docs](http://doc.trolltech.com/latest/qwebpage.html#details). I think the easiest way to do what you want is to use the [QtXml](http://doc.trolltech.com/latest/qtxml.html) module directly instead of getting the DOM tree through QtWebKit.
Job
@Job Actual webpages are better parsed by actual browser.
Xolve