Simple Selector ExampleThe Simple Selector example shows how to use QWebElement to access the Document Object Model (DOM) in a Web page. [Missing image webkit-simpleselector.png] The QWebElement class enables access to the document structure and content in a Web page, as represented by a QWebFrame instance. It can be used for basic traversal of the document structure (see the DOM Traversal Example), to search for particular elements, and to modify any elements found. This example uses a QWebView widget to display a Web page. A QLineEdit widget and QPushButton allow the user to enter a query and highlight the results in the page. These widgets are contained in an instance of the Window class, which we described below. Window Class DefinitionThe Window class describes the example's user interface and this is partially described by the window.ui file, created using Qt Designer: We use multiple inheritance to include the user interface description. We define slots that will automatically respond to signals emitted by certain user interface controls. Window Class ImplementationSince the layout of the user interface is provided by the window.ui user interface file, we only need to call the setupUi() in the constructor: This adds all the controls to the window and sets up connections between their signals and suitably-named slots in the Window class. The QLineEdit instance was given a name of elementLineEdit in Qt Designer, so the on_elementLineEdit_returnPressed() slot is automatically connected to its returnPressed() signal. This slot performs the main work of this example. We begin by obtaining a QWebFrame instance for the current page shown in the QWebView widget. Each QWebFrame contains a QWebElement instance that represents the document, and we obtain this in order to examine its contents: Taking the contents of the QLineEdit as the query text, we call the element's findAll() function to obtain a list of elements that match the query. For each element obtained, we modify its style by setting its style attribute to give it a yellow background color. Since we also want the query to be performed when the user clicks the Highlight button, we also implement the on_highlightButton_clicked() slot to simply call the on_elementLineEdit_returnPressed() slot when it is invoked: For completeness, we also implement a setUrl() function which simply passes on a QUrl instance to the equivalent function in the QWebView widget: Starting the ExampleThe main function implementation is simple. We set up the application, create a Window instance, set its URL, and show it: When the application's event loop is run, the WebKit home page will load, and the user can then begin to start running queries against the contents of the page. The highlighting can only be removed by reloading the page. To do this, open a context menu over the page and select the Reload menu item. Further ReadingThe QWebElement documentation contains more information about DOM access for the QtWebKit classes. In this example, we take advantage of Qt's auto-connection feature to avoid explicitly connecting signals to slots. |