source: trunk/doc/src/examples/domtraversal.qdoc@ 569

Last change on this file since 569 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 6.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 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:LGPL$
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
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \example webkit/domtraversal
44 \title DOM Traversal Example
45
46 The DOM Traversal example shows how to use the QWebElement class to access
47 the structure of a Web page.
48
49 \image webkit-domtraversal.png
50
51 The QWebElement class provides an API that can be used to examine the structure
52 and content of a Web page via a Document Object Model (DOM) interface. It can be
53 used for basic traversal of the document structure, to search for particular
54 elements (see the \l{Simple Selector Example}), and to modify content in-place.
55
56 This example uses a QWebView widget to display the Web page, and a dock widget
57 holds the QTreeWidget that shows the document structure. These widgets are
58 placed in an instance of the \c Window class, which we describe below.
59
60 \section1 Window Class Definition
61
62 The \c Window class is derived from QMainWindow and its user interface is created
63 using \l{Qt Designer}. As a result, the class is also derived from the user
64 interface class created by \l uic:
65
66 \snippet examples/webkit/domtraversal/window.h Window class definition
67
68 Two important functions to note are the \c on_webView_loadFinished() slot and
69 the \c examineChildElements() function. The former is automatically called
70 when the QWebView widget finishes loading a page \mdash see the
71 \l{#Further Reading}{Further Reading} section for more information on this
72 mechanism.
73
74 The \c examineChildElements() function is used to traverse the document structure
75 and add items to the QTreeWidget.
76
77 \section1 Window Class Implementation
78
79 In the \c Window class constructor, we call the \l{QWidget::}{setupUi()} function
80 to set up the user interface described in the \c{window.ui} file:
81
82 \snippet examples/webkit/domtraversal/window.cpp Window constructor
83
84 When the Web page is loaded, the \c on_webView_loadFinished() slot is called. Here,
85 we clear the tree widget and begin inspection of the document by obtaining the
86 document element from the page's main frame:
87
88 \snippet examples/webkit/domtraversal/window.cpp begin document inspection
89
90 At this point, we call the \c examineChildElements() function to traverse the
91 document, starting with the child elements of the document element for which we
92 will create top level items in the tree widget.
93
94 The \c examineChildElements() function accepts a parent element and a parent item.
95 Starting with the first child element, which we obtain with the element's
96 \l{QWebElement::}{firstChild()} function, we examine each child element of the
97 parent item. For each valid (non-null) element, which we check by calling its
98 \l{QWebElement::}{isNull()} function, we create a new QTreeWidgetItem instance with
99 the element name and add it to the parent item.
100
101 \snippet examples/webkit/domtraversal/window.cpp traverse document
102
103 We recursively examine the child elements for each element by calling
104 \c examineChildElements() with the current child element and the newly-created item.
105 To obtain the next element at the same level in the document, we call its
106 \l{QWebElement::}{nextSibling()} function.
107
108 This recursive approach to reading the document makes it easy to create a simple
109 representation of the document structure in a tree widget.
110
111 For completeness, we show the \c setUrl() function, which is provided to allow the
112 document URL to be set from the example's \c main() function.
113
114 \snippet examples/webkit/domtraversal/window.cpp set URL
115
116 \section1 Starting the Example
117
118 We set up the application, create
119 a \c Window instance, set its URL, and show it:
120
121 \snippet examples/webkit/simpleselector/main.cpp main program
122
123 When the application's event loop is run, the Qt home page will load, and the
124 tree widget will be updated to show the document structure. Navigating to another
125 page will cause the tree widget to be updated to show the document structure of
126 the new page.
127
128 \section1 Further Reading
129
130 The QWebElement documentation contains more information about DOM access for the
131 QtWebKit classes.
132
133 In this example, we take advantage of Qt's
134 \l{Using a Designer UI File in Your Application#Automatic Connections}{auto-connection}
135 feature to avoid explicitly connecting signals to slots. The user interface
136 contains a QWebView widget called \c webView whose \l{QWebView::}{loadFinished()}
137 signal is automatically connected to the \c on_webView_loadFinished() slot when
138 we call \l{QWidget::}{setupUi()} in the \c Window constructor.
139*/
Note: See TracBrowser for help on using the repository browser.