In this example project, we will explore the basic features of the web engine module in Qt and try building a simple working web browser. Since Qt 5.6, Qt's WebKit module has been deprecated and replaced by the WebEngine module, which is based on Google's Chromium engine. Note that when this chapter was written, WebEngine was still heavily under development and may be subject to changes in the near future.
First, let's set up our web engine project:
.pro
) and add the following modules to your project:QT += core gui webengine webenginewidgets
mainwindow.ui
and remove the menuBar
, mainToolBar
, and statusBar
objects, as we don't need those in this project:mainwindow.h
and mainwindow.cpp
. Repeat this step for all the other buttons as well.mainwindow.h
and mainwindow.cpp
.mainwindow.h
. The first thing we need to do is to add the following header to mainwindow.h
:#include <QtWebEngineWidgets/QtWebEngineWidgets>
loadUrl()
function under the class destructor:public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void loadUrl();
loading()
to mainwindow.h
as we'll be using it pretty soon:private slots:
void on_goButton_clicked();
void on_address_returnPressed();
void on_backButton_clicked();
void on_forwardButton_clicked();
void loading(int progress);
QWebEngineView
object and call it webview
:private:
Ui::MainWindow *ui;
QWebEngineView* webview;
mainwindow.cpp
and initiate the web engine view. Then, add it to the second horizontal layout and connect its loadProgress()
signal to the loading()
slot function we just added to mainwindow.h
:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); webview = new QWebEngineView; ui->horizontalLayout_2->addWidget(webview); connect(webview, SIGNAL(loadProgress(int)), SLOT(loading(int))); }
loadUrl()
function is called:void MainWindow::loadUrl() { QUrl url = QUrl(ui->address->text()); url.setScheme("http"); webview->page()->load(url); }
loadUrl()
function when the Go button is clicked or when the Return/Enter
key is clicked:void MainWindow::on_goButton_clicked() { loadUrl(); } void MainWindow::on_address_returnPressed() { loadUrl(); }
void MainWindow::on_backButton_clicked() { webview->back(); } void MainWindow::on_forwardButton_clicked() { webview->forward(); }
progressBar
when the web page is being loaded:void MainWindow::loading(int progress) { ui->progressBar->setValue(progress); }
The old web view system was based on Apple's WebKit engine and only available in Qt 5.5 and its predecessor. Since 5.6, WebKit has been completely abandoned by Qt and replaced with Google's Chromium engine. The API has been completely changed and therefore all the code related to Qt WebKit will not work correctly once migrated to 5.6. If you're new to Qt, it's recommended to skip WebKit and learn the WebEngine API since it is becoming the new standard in Qt. If you have used Qt's WebKit in the past, this web page teaches you how to port your old code over to WebEngine, https://wiki.qt.io/Porting_from_QtWebKit_to_QtWebEngine.
In Step 16, we connected the loadProgress()
signal that belongs to the web view widget to the loading()
slot function. The signal will be called automatically when the web view is loading the web page you requested by calling QWebEnginePage::load()
in Step 17. You can also connect the loadStarted()
and loadFinished()
signals as well if you need to.
In Step 17, we used the QUrl
class to convert the text obtained from the line edit to URL format. By default, the address we inserted will lead to the local path if we do not specify the URL scheme (http
, https
, ftp
, and so on). We may not be able to load the page if, say, we gave it packtpub.com
instead of http://packtpub.com
. Therefore, we manually specify a URL scheme for it by calling QUrl::setScheme()
. This will ensure the address is properly formatted before passing it to the web view.
18.191.144.194