WebEngine Widgets Minimal Example

WebEngine Widgets Minimal Example demonstrates how to use QWebEngineView to render a web page. It shows the minimum amount of code needed to load and display an HTML page, and can be used as a basis for further experimentation.

Running the Example

To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

The Code

In the main function we first set the Qt::AA_EnableHighDpiScaling. This lets the web view automatically scale on high-dpi displays.

Next, we instantiate a QApplication and a QWebEngineView. The URL to load is set by calling QWebEngineView::setUrl. The view widget is given a reasonable default size, and shown. Finally, QApplication::exec() launches the main event loop.

#include <QApplication>
#include <QWebEngineView>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);

    QWebEngineView view;
    view.setUrl(QUrl(QStringLiteral("http://www.qt.io")));
    view.resize(1024, 750);
    view.show();

    return app.exec();
}

Requirements

The example requires a working internet connection to render the Qt Homepage. An optional system proxy should be picked up automatically. However, for proxies that require a username or password, you need to connect to QWebEnginePage::proxyAuthenticationRequired.

Qt WebEngine Widgets uses the Qt Quick Scene Graph to compose the page. Therefore, OpenGL support is required.

Files:

© 2017 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.