Introduction to Qt WebEngine

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.

How to do it…

First, let's set up our web engine project:

  1. First, you are required to download and install Microsoft Visual Studio if you do not have it installed on your computer. This is because at the moment, Qt's WebEngine module only works with the Visual C++ compiler and not others, such as MinGW or Clang. This might change in the future, but it all depends on whether Google wants to make their Chromium engine support other compilers or not. Meanwhile, you can download the latest Visual Studio from here: https://www.visualstudio.com.
  2. At the same time, you may also need to make sure that the Qt you installed on your computer supports the Visual C++ compiler. You can add the mvc2015 component to your Qt installation using Qt's maintenance tool. Also, make sure that you have installed the Qt WebEngine component in your Qt as well:
    How to do it…
  3. Once you are done with that, open up Qt Creator and create a new Qt Widgets Application project. This time, you must select a kit that uses the Visual C++ compiler:
    How to do it…
  4. After that, open up your project file (.pro) and add the following modules to your project:
    QT += core gui webengine webenginewidgets
    
  5. Open up mainwindow.ui and remove the menuBar, mainToolBar, and statusBar objects, as we don't need those in this project:
    How to do it…
  6. Place two horizontal layouts on the canvas, then place a line edit widget and a push button for the layout at the top:
    How to do it…
  7. After that, select the canvas and click on the Lay Out Vertically button located at the top of the editor:
    How to do it…
  8. Once you have clicked on the Lay Out Vertically button, the layouts will expand and follow the size of the main window. The line edit will also expand horizontally based on the width of the horizontal layout:
    How to do it…
  9. Next, add two buttons to the left side of the line edit. We'll use these two buttons to move back and forward between page histories. Then, add a Progress bar widget at the bottom of the main window so that we can find out whether the page has finished loading, or loading is still in progress. We don't have to worry about the horizontal layout in the middle at this point, as we'll be adding the web view to it later using C++ code, and the space will then be occupied:
    How to do it…
  10. Right-click on one of the buttons and select Go to slot…, then select clicked() and click OK. A slot function will be automatically created for you in mainwindow.h and mainwindow.cpp. Repeat this step for all the other buttons as well.
  11. After that, right-click on the line edit and select Go to slot…, then select returnPressed() and click OK. Another slot function will now be automatically created for you in mainwindow.h and mainwindow.cpp.
  12. Now that we are done with our UI design, let's hop over to mainwindow.h. The first thing we need to do is to add the following header to mainwindow.h:
    #include <QtWebEngineWidgets/QtWebEngineWidgets>
  13. Then, declare loadUrl() function under the class destructor:
    public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
    
      void loadUrl();
    
  14. After that, add a custom slot function called 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);
    
  15. Lastly, declare a QWebEngineView object and call it webview:
    private:
      Ui::MainWindow *ui;
      QWebEngineView* webview;
    
  16. Once you're done with that, open up 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)));
    }
  17. After that, we declare what will happen when the loadUrl() function is called:
    void MainWindow::loadUrl()
    {
      QUrl url = QUrl(ui->address->text());
      url.setScheme("http");
      webview->page()->load(url);
    }
  18. Next, call the 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();
    }
  19. As for the other two buttons, we'll ask the web view to load the previous page or the next page if it is available in the history stack:
    void MainWindow::on_backButton_clicked()
    {
      webview->back();
    }
    
    void MainWindow::on_forwardButton_clicked()
    {
      webview->forward();
    }
  20. Lastly, change the value of the progressBar when the web page is being loaded:
    void MainWindow::loading(int progress)
    {
      ui->progressBar->setValue(progress);
    }
  21. Build and run the program now and you will get a very basic but functional web browser!
    How to do it…

How it works…

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.

There's more…

If you're running Qt 5.6 or above and for some reason you need the WebKit module for your project (usually for maintaining an old project), you can obtain the module code from GitHub and build it by yourself:

https://github.com/qt/qtwebkit

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.144.194