7

I have never used Qt and WebKit and now have a need to create a simple single web page browser using the Qt WebKit module. The application that I'm looking to create needs to have a plain window that displays a web page URL passed in via command line. I've done this sort of thing using WebKitGTK but I have no idea where to start in Qt.

I've done some research to see what is involved and so far I've only been able to find snippets of code that pertain to the WebKit QWebView class.

So can anybody provide me with complete sample code that will just display a web page in Qt? Once I get that part down I will be able to continue and extend from there and continue to learn about Qt and WebKit.

I will offer up a great deal of bounty points for some excellent help.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Chimera
  • 5,884
  • 7
  • 49
  • 81

1 Answers1

20

Your requirement is still not scoped enough. If you want the simplest possible full application example which displays a web page, here is the code:

#include <QtGui>
#include <QtWebKit>

int main(int argc, char** argv) {
    QApplication app(argc, argv);
    QWebView view;
    view.show();
    view.setUrl(QUrl("http://google.com"));
    return app.exec();
}

If that is example.cpp, you can use the following example.pro:

QT += webkit
SOURCES = example.cpp

The easiest way to Qt development is using Qt Creator and you can load that .pro file with Qt Creator, build the app, and launch it. There is only one window (QWebView instance) and it will open Google homepage.

Ariya Hidayat
  • 12,523
  • 3
  • 46
  • 39
  • Well this works great using QtWebKit-47. However, it doesn't seems to be able to play videos using HTML5 video tags. Any idea what the issue may be? I'm running on CentOS 5.8. Regards. – Chimera Jan 31 '13 at 22:30
  • Probably codecs issue or some other related stuff. You should post a new question which is more specific and better scoped. – Ariya Hidayat Feb 01 '13 at 06:34
  • @AriyaHidayat This doesn't work in Qt 5.9.4 -- is there a change that has happened in Qt? – developer01 Jan 14 '21 at 17:56