0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 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 xmlpatterns/recipes
|
|
44 |
\title Recipes Example
|
|
45 |
|
|
46 |
The recipes example shows how to use QtXmlPatterns to query XML data
|
|
47 |
loaded from a file.
|
|
48 |
|
|
49 |
\tableofcontents
|
|
50 |
|
|
51 |
\section1 Introduction
|
|
52 |
|
|
53 |
In this case, the XML data represents a cookbook, \c{cookbook.xml},
|
|
54 |
which contains \c{<cookbook>} as its document element, which in turn
|
|
55 |
contains a sequence of \c{<recipe>} elements. This XML data is
|
|
56 |
searched using queries stored in XQuery files (\c{*.xq}).
|
|
57 |
|
|
58 |
\section2 The User Interface
|
|
59 |
|
|
60 |
The UI for this example was created using \l{Qt Designer Manual} {Qt
|
|
61 |
Designer}:
|
|
62 |
|
|
63 |
\image recipes-example.png
|
|
64 |
|
|
65 |
The UI consists of three \l{QGroupBox} {group boxes} arranged
|
|
66 |
vertically. The top one contains a \l{QTextEdit} {text viewer} that
|
|
67 |
displays the XML text from the cookbook file. The middle group box
|
|
68 |
contains a \l{QComboBox} {combo box} for choosing the \l{A Short
|
|
69 |
Path to XQuery} {XQuery} to run and a \l{QTextEdit} {text viewer}
|
|
70 |
for displaying the text of the selected XQuery. The \c{.xq} files in
|
|
71 |
the file list above are shown in the combo box menu. Choosing an
|
|
72 |
XQuery loads, parses, and runs the selected XQuery. The query result
|
|
73 |
is shown in the bottom group box's \l{QTextEdit} {text viewer}.
|
|
74 |
|
|
75 |
\section2 Running your own XQueries
|
|
76 |
|
|
77 |
You can write your own XQuery files and run them in the example
|
|
78 |
program. The file \c{xmlpatterns/recipes/recipes.qrc} is the \l{The
|
|
79 |
Qt Resource System} {resource file} for this example. It is used in
|
|
80 |
\c{main.cpp} (\c{Q_INIT_RESOURCE(recipes);}). It lists the XQuery
|
|
81 |
files (\c{.xq}) that can be selected in the combobox.
|
|
82 |
|
|
83 |
\quotefromfile examples/xmlpatterns/recipes/recipes.qrc
|
|
84 |
\printuntil
|
|
85 |
|
|
86 |
To add your own queries to the example's combobox, store your
|
|
87 |
\c{.xq} files in the \c{examples/xmlpatterns/recipes/files}
|
|
88 |
directory and add them to \c{recipes.qrc} as shown above.
|
|
89 |
|
|
90 |
\section1 Code Walk-Through
|
|
91 |
|
|
92 |
The example's main() function creates the standard instance of
|
|
93 |
QApplication. Then it creates an instance of the UI class, shows it,
|
|
94 |
and starts the Qt event loop:
|
|
95 |
|
|
96 |
\snippet examples/xmlpatterns/recipes/main.cpp 0
|
|
97 |
|
|
98 |
\section2 The UI Class: QueryMainWindow
|
|
99 |
|
|
100 |
The example's UI is a conventional Qt GUI application inheriting
|
|
101 |
QMainWindow and the class generated by \l{Qt Designer Manual} {Qt
|
|
102 |
Designer}:
|
|
103 |
|
|
104 |
\snippet examples/xmlpatterns/recipes/querymainwindow.h 0
|
|
105 |
|
|
106 |
The constructor finds the window's \l{QComboBox} {combo box} child
|
|
107 |
widget and connects its \l{QComboBox::currentIndexChanged()}
|
|
108 |
{currentIndexChanged()} signal to the window's \c{displayQuery()}
|
|
109 |
slot. It then calls \c{loadInputFile()} to load \c{cookbook.xml} and
|
|
110 |
display its contents in the top group box's \l{QTextEdit} {text
|
|
111 |
viewer} . Finally, it finds the XQuery files (\c{.xq}) and adds each
|
|
112 |
one to the \l{QComboBox} {combo box} menu.
|
|
113 |
|
|
114 |
\snippet examples/xmlpatterns/recipes/querymainwindow.cpp 0
|
|
115 |
|
|
116 |
The work is done in the \l{displayQuery() slot} {displayQuery()}
|
|
117 |
slot and the \l{evaluate() function} {evaluate()} function it
|
|
118 |
calls. \l{displayQuery() slot} {displayQuery()} loads and displays
|
|
119 |
the selected query file and passes the XQuery text to \l{evaluate()
|
|
120 |
function} {evaluate()}.
|
|
121 |
|
|
122 |
\target displayQuery() slot
|
|
123 |
\snippet examples/xmlpatterns/recipes/querymainwindow.cpp 1
|
|
124 |
|
|
125 |
\l{evaluate() function} {evaluate()} demonstrates the standard
|
|
126 |
QtXmlPatterns usage pattern. First, an instance of QXmlQuery is
|
|
127 |
created (\c{query}). The \c{query's} \l{QXmlQuery::bindVariable()}
|
|
128 |
{bindVariable()} function is then called to bind the \c cookbook.xml
|
|
129 |
file to the XQuery variable \c inputDocument. \e{After} the variable
|
|
130 |
is bound, \l{QXmlQuery::setQuery()} {setQuery()} is called to pass
|
|
131 |
the XQuery text to the \c query.
|
|
132 |
|
|
133 |
\note \l{QXmlQuery::setQuery()} {setQuery()} must be called
|
|
134 |
\e{after} \l{QXmlQuery::bindVariable()} {bindVariable()}.
|
|
135 |
|
|
136 |
Passing the XQuery to \l{QXmlQuery::setQuery()} {setQuery()} causes
|
|
137 |
QtXmlPatterns to parse the XQuery. \l{QXmlQuery::isValid()} is
|
|
138 |
called to ensure that the XQuery was correctly parsed.
|
|
139 |
|
|
140 |
\target evaluate() function
|
|
141 |
\snippet examples/xmlpatterns/recipes/querymainwindow.cpp 2
|
|
142 |
|
|
143 |
If the XQuery is valid, an instance of QXmlFormatter is created to
|
|
144 |
format the query result as XML into a QBuffer. To evaluate the
|
|
145 |
XQuery, an overload of \l{QXmlQuery::evaluateTo()} {evaluateTo()} is
|
|
146 |
called that takes a QAbstractXmlReceiver for its output
|
|
147 |
(QXmlFormatter inherits QAbstractXmlReceiver). Finally, the
|
|
148 |
formatted XML result is displayed in the UI's bottom text view.
|
|
149 |
|
|
150 |
\note Each XQuery \c{.xq} file must declare the \c{$inputDocument}
|
|
151 |
variable to represent the \c cookbook.xml document:
|
|
152 |
|
|
153 |
\code
|
|
154 |
(: All ingredients for Mushroom Soup. :)
|
|
155 |
declare variable $inputDocument external;
|
|
156 |
|
|
157 |
doc($inputDocument)/cookbook/recipe[@xml:id = "MushroomSoup"]/ingredient/
|
|
158 |
<p>{@name, @quantity}</p>
|
|
159 |
\endcode
|
|
160 |
|
|
161 |
\note If you add add your own query.xq files, you must declare the
|
|
162 |
\c{$inputDocument} and use it as shown above.
|
|
163 |
|
|
164 |
*/
|