diff options
Diffstat (limited to 'doc/src/examples')
-rw-r--r-- | doc/src/examples/domtraversal.qdoc | 139 | ||||
-rw-r--r-- | doc/src/examples/simpleselector.qdoc | 128 |
2 files changed, 267 insertions, 0 deletions
diff --git a/doc/src/examples/domtraversal.qdoc b/doc/src/examples/domtraversal.qdoc new file mode 100644 index 0000000..57e61d0 --- /dev/null +++ b/doc/src/examples/domtraversal.qdoc @@ -0,0 +1,139 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example webkit/domtraversal + \title DOM Traversal Example + + The DOM Traversal example shows how to use the QWebElement class to access + the structure of a Web page. + + \image webkit-domtraversal.png + + The QWebElement class provides an API that can be used to examine the structure + and content of a Web page via a Document Object Model (DOM) interface. It can be + used for basic traversal of the document structure, to search for particular + elements (see the \l{Simple Selector Example}), and to modify content in-place. + + This example uses a QWebView widget to display the Web page, and a dock widget + holds the QTreeWidget that shows the document structure. These widgets are + placed in an instance of the \c Window class, which we describe below. + + \section1 Window Class Definition + + The \c Window class is derived from QMainWindow and its user interface is created + using \l{Qt Designer}. As a result, the class is also derived from the user + interface class created by \l uic: + + \snippet examples/webkit/domtraversal/window.h Window class definition + + Two important functions to note are the \c on_webView_loadFinished() slot and + the \c examineChildElements() function. The former is automatically called + when the QWebView widget finishes loading a page \mdash see the + \l{#Further Reading}{Further Reading} section for more information on this + mechanism. + + The \c examineChildElements() function is used to traverse the document structure + and add items to the QTreeWidget. + + \section1 Window Class Implementation + + In the \c Window class constructor, we call the \l{QWidget::}{setupUi()} function + to set up the user interface described in the \c{window.ui} file: + + \snippet examples/webkit/domtraversal/window.cpp Window constructor + + When the Web page is loaded, the \c on_webView_loadFinished() slot is called. Here, + we clear the tree widget and begin inspection of the document by obtaining the + document element from the page's main frame: + + \snippet examples/webkit/domtraversal/window.cpp begin document inspection + + At this point, we call the \c examineChildElements() function to traverse the + document, starting with the child elements of the document element for which we + will create top level items in the tree widget. + + The \c examineChildElements() function accepts a parent element and a parent item. + Starting with the first child element, which we obtain with the element's + \l{QWebElement::}{firstChild()} function, we examine each child element of the + parent item. For each valid (non-null) element, which we check by calling its + \l{QWebElement::}{isNull()} function, we create a new QTreeWidgetItem instance with + the element name and add it to the parent item. + + \snippet examples/webkit/domtraversal/window.cpp traverse document + + We recursively examine the child elements for each element by calling + \c examineChildElements() with the current child element and the newly-created item. + To obtain the next element at the same level in the document, we call its + \l{QWebElement::}{nextSibling()} function. + + This recursive approach to reading the document makes it easy to create a simple + representation of the document structure in a tree widget. + + For completeness, we show the \c setUrl() function, which is provided to allow the + document URL to be set from the example's \c main() function. + + \snippet examples/webkit/domtraversal/window.cpp set URL + + \section1 Starting the Example + + We set up the application, create + a \c Window instance, set its URL, and show it: + + \snippet examples/webkit/simpleselector/main.cpp main program + + When the application's event loop is run, the Qt home page will load, and the + tree widget will be updated to show the document structure. Navigating to another + page will cause the tree widget to be updated to show the document structure of + the new page. + + \section1 Further Reading + + The QWebElement documentation contains more information about DOM access for the + QtWebKit classes. + + In this example, we take advantage of Qt's + \l{Using a Designer UI File in Your Application#Automatic Connections}{auto-connection} + feature to avoid explicitly connecting signals to slots. The user interface + contains a QWebView widget called \c webView whose \l{QWebView::}{loadFinished()} + signal is automatically connected to the \c on_webView_loadFinished() slot when + we call \l{QWidget::}{setupUi()} in the \c Window constructor. +*/ diff --git a/doc/src/examples/simpleselector.qdoc b/doc/src/examples/simpleselector.qdoc new file mode 100644 index 0000000..7db6576 --- /dev/null +++ b/doc/src/examples/simpleselector.qdoc @@ -0,0 +1,128 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example webkit/simpleselector + \title Simple Selector Example + + The Simple Selector example shows how to use QWebElement to access the + Document Object Model (DOM) in a Web page. + + \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 \l{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 \c Window class, which we described below. + + \section1 Window Class Definition + + The \c Window class describes the example's user interface and this is partially described + by the \c window.ui file, created using \l{Qt Designer}: + + \snippet examples/webkit/simpleselector/window.h Window class definition + + We use \l{Using a Designer UI File in Your Application#The Multiple Inheritance Approach} + {multiple inheritance} to include the user interface description. We define slots that + will automatically respond to signals emitted by certain user interface controls. + + \section1 Window Class Implementation + + Since the layout of the user interface is provided by the \c{window.ui} user interface file, + we only need to call the \l{QWidget::}{setupUi()} in the constructor: + + \snippet examples/webkit/simpleselector/window.cpp Window class constructor + + This adds all the controls to the window and sets up connections between their signals + and suitably-named slots in the \c Window class. The QLineEdit instance was given a name of + \c elementLineEdit in Qt Designer, so the \c{on_elementLineEdit_returnPressed()} slot is + automatically connected to its \l{QLineEdit::}{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: + + \snippet examples/webkit/simpleselector/window.cpp return pressed + + Taking the contents of the QLineEdit as the query text, we call the element's + \l{QWebElement::}{findAll()} function to obtain a list of elements that match the + query. + + For each element obtained, we modify its style by setting its \c style attribute + to give it a yellow background color. + + Since we also want the query to be performed when the user clicks the \gui Highlight + button, we also implement the \c{on_highlightButton_clicked()} slot to simply call + the \c{on_elementLineEdit_returnPressed()} slot when it is invoked: + + \snippet examples/webkit/simpleselector/window.cpp button clicked + + For completeness, we also implement a \c setUrl() function which simply passes on + a QUrl instance to the equivalent function in the QWebView widget: + + \snippet examples/webkit/simpleselector/window.cpp set URL + + \section1 Starting the Example + + The main function implementation is simple. We set up the application, create + a \c Window instance, set its URL, and show it: + + \snippet examples/webkit/simpleselector/main.cpp main program + + 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 \gui Reload menu item. + + \section1 Further Reading + + The QWebElement documentation contains more information about DOM access for the + QtWebKit classes. + + In this example, we take advantage of Qt's + \l{Using a Designer UI File in Your Application#Automatic Connections}{auto-connection} + feature to avoid explicitly connecting signals to slots. +*/ |