doc/src/examples/simpleselector.qdoc
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 /*!
       
    43     \example webkit/simpleselector
       
    44     \title Simple Selector Example
       
    45 
       
    46     The Simple Selector example shows how to use QWebElement to access the
       
    47     Document Object Model (DOM) in a Web page.
       
    48 
       
    49     \image webkit-simpleselector.png
       
    50 
       
    51     The QWebElement class enables access to the document structure and content in a Web page,
       
    52     as represented by a QWebFrame instance. It can be used for basic traversal of the document
       
    53     structure (see the \l{DOM Traversal Example}), to search for particular elements, and to
       
    54     modify any elements found.
       
    55 
       
    56     This example uses a QWebView widget to display a Web page. A QLineEdit widget and QPushButton
       
    57     allow the user to enter a query and highlight the results in the page. These widgets are
       
    58     contained in an instance of the \c Window class, which we described below.
       
    59 
       
    60     \section1 Window Class Definition
       
    61 
       
    62     The \c Window class describes the example's user interface and this is partially described
       
    63     by the \c window.ui file, created using \l{Qt Designer}:
       
    64 
       
    65     \snippet examples/webkit/simpleselector/window.h Window class definition
       
    66 
       
    67     We use \l{Using a Designer UI File in Your Application#The Multiple Inheritance Approach}
       
    68     {multiple inheritance} to include the user interface description. We define slots that
       
    69     will automatically respond to signals emitted by certain user interface controls.
       
    70 
       
    71     \section1 Window Class Implementation
       
    72 
       
    73     Since the layout of the user interface is provided by the \c{window.ui} user interface file,
       
    74     we only need to call the \l{QWidget::}{setupUi()} in the constructor:
       
    75 
       
    76     \snippet examples/webkit/simpleselector/window.cpp Window class constructor
       
    77 
       
    78     This adds all the controls to the window and sets up connections between their signals
       
    79     and suitably-named slots in the \c Window class. The QLineEdit instance was given a name of
       
    80     \c elementLineEdit in Qt Designer, so the \c{on_elementLineEdit_returnPressed()} slot is
       
    81     automatically connected to its \l{QLineEdit::}{returnPressed()} signal.
       
    82 
       
    83     This slot performs the main work of this example. We begin by obtaining a QWebFrame
       
    84     instance for the current page shown in the QWebView widget. Each QWebFrame contains
       
    85     a QWebElement instance that represents the document, and we obtain this in order to
       
    86     examine its contents:
       
    87 
       
    88     \snippet examples/webkit/simpleselector/window.cpp return pressed
       
    89 
       
    90     Taking the contents of the QLineEdit as the query text, we call the element's
       
    91     \l{QWebElement::}{findAll()} function to obtain a list of elements that match the
       
    92     query.
       
    93 
       
    94     For each element obtained, we modify its style by setting its \c style attribute
       
    95     to give it a yellow background color.
       
    96 
       
    97     Since we also want the query to be performed when the user clicks the \gui Highlight
       
    98     button, we also implement the \c{on_highlightButton_clicked()} slot to simply call
       
    99     the \c{on_elementLineEdit_returnPressed()} slot when it is invoked:
       
   100 
       
   101     \snippet examples/webkit/simpleselector/window.cpp button clicked
       
   102 
       
   103     For completeness, we also implement a \c setUrl() function which simply passes on
       
   104     a QUrl instance to the equivalent function in the QWebView widget:
       
   105 
       
   106     \snippet examples/webkit/simpleselector/window.cpp set URL
       
   107 
       
   108     \section1 Starting the Example
       
   109 
       
   110     The main function implementation is simple. We set up the application, create
       
   111     a \c Window instance, set its URL, and show it:
       
   112 
       
   113     \snippet examples/webkit/simpleselector/main.cpp main program
       
   114 
       
   115     When the application's event loop is run, the WebKit home page will load, and the
       
   116     user can then begin to start running queries against the contents of the page.
       
   117     The highlighting can only be removed by reloading the page. To do this, open a
       
   118     context menu over the page and select the \gui Reload menu item.
       
   119 
       
   120     \section1 Further Reading
       
   121 
       
   122     The QWebElement documentation contains more information about DOM access for the
       
   123     QtWebKit classes.
       
   124 
       
   125     In this example, we take advantage of Qt's
       
   126     \l{Using a Designer UI File in Your Application#Automatic Connections}{auto-connection}
       
   127     feature to avoid explicitly connecting signals to slots.
       
   128 */