Example code for a simple web page browser using W

2020-06-04 05:42发布

问题:

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.

回答1:

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.