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

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
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 qt-info@nokia.com.
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29 \example webkit/domtraversal
30 \title DOM Traversal Example
31
32 The DOM Traversal example shows how to use the QWebElement class to access
33 the structure of a Web page.
34
35 \image webkit-domtraversal.png
36
37 The QWebElement class provides an API that can be used to examine the structure
38 and content of a Web page via a Document Object Model (DOM) interface. It can be
39 used for basic traversal of the document structure, to search for particular
40 elements (see the \l{Simple Selector Example}), and to modify content in-place.
41
42 This example uses a QWebView widget to display the Web page, and a dock widget
43 holds the QTreeWidget that shows the document structure. These widgets are
44 placed in an instance of the \c Window class, which we describe below.
45
46 \section1 Window Class Definition
47
48 The \c Window class is derived from QMainWindow and its user interface is created
49 using \l{Qt Designer}. As a result, the class is also derived from the user
50 interface class created by \l uic:
51
52 \snippet examples/webkit/domtraversal/window.h Window class definition
53
54 Two important functions to note are the \c on_webView_loadFinished() slot and
55 the \c examineChildElements() function. The former is automatically called
56 when the QWebView widget finishes loading a page \mdash see the
57 \l{#Further Reading}{Further Reading} section for more information on this
58 mechanism.
59
60 The \c examineChildElements() function is used to traverse the document structure
61 and add items to the QTreeWidget.
62
63 \section1 Window Class Implementation
64
65 In the \c Window class constructor, we call the \l{QWidget::}{setupUi()} function
66 to set up the user interface described in the \c{window.ui} file:
67
68 \snippet examples/webkit/domtraversal/window.cpp Window constructor
69
70 When the Web page is loaded, the \c on_webView_loadFinished() slot is called. Here,
71 we clear the tree widget and begin inspection of the document by obtaining the
72 document element from the page's main frame:
73
74 \snippet examples/webkit/domtraversal/window.cpp begin document inspection
75
76 At this point, we call the \c examineChildElements() function to traverse the
77 document, starting with the child elements of the document element for which we
78 will create top level items in the tree widget.
79
80 The \c examineChildElements() function accepts a parent element and a parent item.
81 Starting with the first child element, which we obtain with the element's
82 \l{QWebElement::}{firstChild()} function, we examine each child element of the
83 parent item. For each valid (non-null) element, which we check by calling its
84 \l{QWebElement::}{isNull()} function, we create a new QTreeWidgetItem instance with
85 the element name and add it to the parent item.
86
87 \snippet examples/webkit/domtraversal/window.cpp traverse document
88
89 We recursively examine the child elements for each element by calling
90 \c examineChildElements() with the current child element and the newly-created item.
91 To obtain the next element at the same level in the document, we call its
92 \l{QWebElement::}{nextSibling()} function.
93
94 This recursive approach to reading the document makes it easy to create a simple
95 representation of the document structure in a tree widget.
96
97 For completeness, we show the \c setUrl() function, which is provided to allow the
98 document URL to be set from the example's \c main() function.
99
100 \snippet examples/webkit/domtraversal/window.cpp set URL
101
102 \section1 Starting the Example
103
104 We set up the application, create
105 a \c Window instance, set its URL, and show it:
106
107 \snippet examples/webkit/simpleselector/main.cpp main program
108
109 When the application's event loop is run, the Qt home page will load, and the
110 tree widget will be updated to show the document structure. Navigating to another
111 page will cause the tree widget to be updated to show the document structure of
112 the new page.
113
114 \section1 Further Reading
115
116 The QWebElement documentation contains more information about DOM access for the
117 QtWebKit classes.
118
119 In this example, we take advantage of Qt's
120 \l{Using a Designer UI File in Your Application#Automatic Connections}{auto-connection}
121 feature to avoid explicitly connecting signals to slots. The user interface
122 contains a QWebView widget called \c webView whose \l{QWebView::}{loadFinished()}
123 signal is automatically connected to the \c on_webView_loadFinished() slot when
124 we call \l{QWidget::}{setupUi()} in the \c Window constructor.
125*/
Note: See TracBrowser for help on using the repository browser.