1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example webkit/simpleselector
|
---|
30 | \title Simple Selector Example
|
---|
31 |
|
---|
32 | The Simple Selector example shows how to use QWebElement to access the
|
---|
33 | Document Object Model (DOM) in a Web page.
|
---|
34 |
|
---|
35 | \image webkit-simpleselector.png
|
---|
36 |
|
---|
37 | The QWebElement class enables access to the document structure and content in a Web page,
|
---|
38 | as represented by a QWebFrame instance. It can be used for basic traversal of the document
|
---|
39 | structure (see the \l{DOM Traversal Example}), to search for particular elements, and to
|
---|
40 | modify any elements found.
|
---|
41 |
|
---|
42 | This example uses a QWebView widget to display a Web page. A QLineEdit widget and QPushButton
|
---|
43 | allow the user to enter a query and highlight the results in the page. These widgets are
|
---|
44 | contained in an instance of the \c Window class, which we described below.
|
---|
45 |
|
---|
46 | \section1 Window Class Definition
|
---|
47 |
|
---|
48 | The \c Window class describes the example's user interface and this is partially described
|
---|
49 | by the \c window.ui file, created using \l{Qt Designer}:
|
---|
50 |
|
---|
51 | \snippet examples/webkit/simpleselector/window.h Window class definition
|
---|
52 |
|
---|
53 | We use \l{Using a Designer UI File in Your Application#The Multiple Inheritance Approach}
|
---|
54 | {multiple inheritance} to include the user interface description. We define slots that
|
---|
55 | will automatically respond to signals emitted by certain user interface controls.
|
---|
56 |
|
---|
57 | \section1 Window Class Implementation
|
---|
58 |
|
---|
59 | Since the layout of the user interface is provided by the \c{window.ui} user interface file,
|
---|
60 | we only need to call the \l{QWidget::}{setupUi()} in the constructor:
|
---|
61 |
|
---|
62 | \snippet examples/webkit/simpleselector/window.cpp Window class constructor
|
---|
63 |
|
---|
64 | This adds all the controls to the window and sets up connections between their signals
|
---|
65 | and suitably-named slots in the \c Window class. The QLineEdit instance was given a name of
|
---|
66 | \c elementLineEdit in Qt Designer, so the \c{on_elementLineEdit_returnPressed()} slot is
|
---|
67 | automatically connected to its \l{QLineEdit::}{returnPressed()} signal.
|
---|
68 |
|
---|
69 | This slot performs the main work of this example. We begin by obtaining a QWebFrame
|
---|
70 | instance for the current page shown in the QWebView widget. Each QWebFrame contains
|
---|
71 | a QWebElement instance that represents the document, and we obtain this in order to
|
---|
72 | examine its contents:
|
---|
73 |
|
---|
74 | \snippet examples/webkit/simpleselector/window.cpp return pressed
|
---|
75 |
|
---|
76 | Taking the contents of the QLineEdit as the query text, we call the element's
|
---|
77 | \l{QWebElement::}{findAll()} function to obtain a list of elements that match the
|
---|
78 | query.
|
---|
79 |
|
---|
80 | For each element obtained, we modify its style by setting its \c style attribute
|
---|
81 | to give it a yellow background color.
|
---|
82 |
|
---|
83 | Since we also want the query to be performed when the user clicks the \gui Highlight
|
---|
84 | button, we also implement the \c{on_highlightButton_clicked()} slot to simply call
|
---|
85 | the \c{on_elementLineEdit_returnPressed()} slot when it is invoked:
|
---|
86 |
|
---|
87 | \snippet examples/webkit/simpleselector/window.cpp button clicked
|
---|
88 |
|
---|
89 | For completeness, we also implement a \c setUrl() function which simply passes on
|
---|
90 | a QUrl instance to the equivalent function in the QWebView widget:
|
---|
91 |
|
---|
92 | \snippet examples/webkit/simpleselector/window.cpp set URL
|
---|
93 |
|
---|
94 | \section1 Starting the Example
|
---|
95 |
|
---|
96 | The main function implementation is simple. We set up the application, create
|
---|
97 | a \c Window instance, set its URL, and show it:
|
---|
98 |
|
---|
99 | \snippet examples/webkit/simpleselector/main.cpp main program
|
---|
100 |
|
---|
101 | When the application's event loop is run, the WebKit home page will load, and the
|
---|
102 | user can then begin to start running queries against the contents of the page.
|
---|
103 | The highlighting can only be removed by reloading the page. To do this, open a
|
---|
104 | context menu over the page and select the \gui Reload menu item.
|
---|
105 |
|
---|
106 | \section1 Further Reading
|
---|
107 |
|
---|
108 | The QWebElement documentation contains more information about DOM access for the
|
---|
109 | QtWebKit classes.
|
---|
110 |
|
---|
111 | In this example, we take advantage of Qt's
|
---|
112 | \l{Using a Designer UI File in Your Application#Automatic Connections}{auto-connection}
|
---|
113 | feature to avoid explicitly connecting signals to slots.
|
---|
114 | */
|
---|