Building a Simple Web Browser in C++ using Qt

This weekend I began working on something called Selenium Factory. Selenium is an application which automates the web browser, usually used for testing purposes. My intent is to build an application which generates Selenium code, enabling a user to type in the website they wish to test, save their various inputs/objects and output the code upon clicking a “generate” button. Currently, there is a FireFox plugin which can accomplish a similar goal, however it does not quite work.

Prior to building the application I decided to layout a “game plan.” My goals are as follows:

  1. Build a (very) basic web browser.
  2. Generate a log of HTML/Javascript objects each person focused on as well as user input.
  3. Generate Selenium code associated with the tests.

This may seem a bit ambitious, but I do not believe goals one or two will be too difficult and I accomplished goal one in about an hour and a half.

Building a Simple Web Browser

Using C++ and the Qt frame work it is easy to build a web browser. I accomplished this using Qt creator, creating a desktop application, adding a QWebViewer, tossing in a few buttons, and a URL edit line and bingo!

Screen Shot 2014-06-08 at 10.16.25 PM

It even comes with a back button.

There were however several “tricks.” For one, I had to edit the .pro and add the webkitwidgets as so:

I believe if you are using anything less than Qt 5.0 you need to use QT += webkit as opposed to webkitwidgets. This was simple enough, but took a while to figure out because much of the documentation is not very clear.

Once that was taken care of all that was left was loading webpages, which is very convenient using Qt:

 Since I was using the ui class all I had to do was access the webView object, load a particular URL, then show it, the parsing is taken care of behind the scenes. Unfortunately, applications requiring adobe flash (or really any plugins) do not work in this browser, of course for my current needs it really is not necessary.

The final steps I took was adding some extra functionality, such as a “start/stop” toggle button for collecting user input, a back button, a generate code and URL input. All of which was accomplished using the following code:

mainwindow.h
mainwindow.cpp

  The code above currently does not have any of the code from goal #2, however it can function as a basic web browser, thereby accomplishing goal one. I can even view my website!

Screen Shot 2014-06-08 at 10.47.27 PM

If you are interested, feel free to view/fork the code in my github repo.

Or continue reading about the project and the next article in the series:

Accessing Web Elements from Qt WebFrame

Recent Articles

One thought on “Building a Simple Web Browser in C++ using Qt

  1. This would look better if the font were not centered 😉 It’s cool that you built a browser. I didn’t even try to follow the code though, since I don’t know anything about that!

Leave a Reply

Your email address will not be published. Required fields are marked *

 characters available

Time limit is exhausted. Please reload the CAPTCHA.