<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- recipes.qdoc --> <title>Qt 4.8: Recipes Example</title> <link rel="stylesheet" type="text/css" href="style/style.css" /> <script src="scripts/jquery.js" type="text/javascript"></script> <script src="scripts/functions.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="style/superfish.css" /> <link rel="stylesheet" type="text/css" href="style/narrow.css" /> <!--[if IE]> <meta name="MSSmartTagsPreventParsing" content="true"> <meta http-equiv="imagetoolbar" content="no"> <![endif]--> <!--[if lt IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie6.css"> <![endif]--> <!--[if IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie7.css"> <![endif]--> <!--[if IE 8]> <link rel="stylesheet" type="text/css" href="style/style_ie8.css"> <![endif]--> <script src="scripts/superfish.js" type="text/javascript"></script> <script src="scripts/narrow.js" type="text/javascript"></script> </head> <body class="" onload="CheckEmptyAndLoadList();"> <div class="header" id="qtdocheader"> <div class="content"> <div id="nav-logo"> <a href="index.html">Home</a></div> <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a> <div id="narrowsearch"></div> <div id="nav-topright"> <ul> <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li> <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li> <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/"> DOC</a></li> <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li> </ul> </div> <div id="shortCut"> <ul> <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li> <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS </a></li> </ul> </div> <ul class="sf-menu" id="narrowmenu"> <li><a href="#">API Lookup</a> <ul> <li><a href="classes.html">Class index</a></li> <li><a href="functions.html">Function index</a></li> <li><a href="modules.html">Modules</a></li> <li><a href="namespaces.html">Namespaces</a></li> <li><a href="qtglobal.html">Global Declarations</a></li> <li><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </li> <li><a href="#">Qt Topics</a> <ul> <li><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li><a href="supported-platforms.html">Supported Platforms</a></li> <li><a href="technology-apis.html">Qt and Key Technologies</a></li> <li><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </li> <li><a href="#">Examples</a> <ul> <li><a href="all-examples.html">Examples</a></li> <li><a href="tutorials.html">Tutorials</a></li> <li><a href="demos.html">Demos</a></li> <li><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </li> </ul> </div> </div> <div class="wrapper"> <div class="hd"> <span></span> </div> <div class="bd group"> <div class="sidebar"> <div class="searchlabel"> Search index:</div> <div class="search" id="sidebarsearch"> <form id="qtdocsearch" action="" onsubmit="return false;"> <fieldset> <input type="text" name="searchstring" id="pageType" value="" /> <div id="resultdialog"> <a href="#" id="resultclose">Close</a> <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span> results:</p> <ul id="resultlist" class="all"> </ul> </div> </fieldset> </form> </div> <div class="box first bottombar" id="lookup"> <h2 title="API Lookup"><span></span> API Lookup</h2> <div id="list001" class="list"> <ul id="ul001" > <li class="defaultLink"><a href="classes.html">Class index</a></li> <li class="defaultLink"><a href="functions.html">Function index</a></li> <li class="defaultLink"><a href="modules.html">Modules</a></li> <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li> <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li> <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </div> </div> <div class="box bottombar" id="topics"> <h2 title="Qt Topics"><span></span> Qt Topics</h2> <div id="list002" class="list"> <ul id="ul002" > <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li class="defaultLink"><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li> <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </div> </div> <div class="box" id="examples"> <h2 title="Examples"><span></span> Examples</h2> <div id="list003" class="list"> <ul id="ul003"> <li class="defaultLink"><a href="all-examples.html">Examples</a></li> <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li> <li class="defaultLink"><a href="demos.html">Demos</a></li> <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </div> </div> </div> <div class="wrap"> <div class="toolbar"> <div class="breadcrumb toolblock"> <ul> <li class="first"><a href="index.html">Home</a></li> <!-- Breadcrumbs go here --> <li><a href="all-examples.html">Examples</a></li> <li>Recipes Example</li> </ul> </div> <div class="toolbuttons toolblock"> <ul> <li id="smallA" class="t_button">A</li> <li id="medA" class="t_button active">A</li> <li id="bigA" class="t_button">A</li> <li id="print" class="t_button"><a href="javascript:this.print();"> <span>Print</span></a></li> </ul> </div> </div> <div class="content mainContent"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#introduction">Introduction</a></li> <li class="level2"><a href="#the-user-interface">The User Interface</a></li> <li class="level2"><a href="#running-your-own-xqueries">Running your own XQueries</a></li> <li class="level1"><a href="#code-walk-through">Code Walk-Through</a></li> <li class="level2"><a href="#the-ui-class-querymainwindow">The UI Class: QueryMainWindow</a></li> </ul> </div> <h1 class="title">Recipes Example</h1> <span class="subtitle"></span> <!-- $$$xmlpatterns/recipes-description --> <div class="descr"> <a name="details"></a> <p>Files:</p> <ul> <li><a href="xmlpatterns-recipes-querymainwindow-cpp.html">xmlpatterns/recipes/querymainwindow.cpp</a></li> <li><a href="xmlpatterns-recipes-querymainwindow-h.html">xmlpatterns/recipes/querymainwindow.h</a></li> <li><a href="xmlpatterns-recipes-files-allrecipes-xq.html">xmlpatterns/recipes/files/allRecipes.xq</a></li> <li><a href="xmlpatterns-recipes-files-cookbook-xml.html">xmlpatterns/recipes/files/cookbook.xml</a></li> <li><a href="xmlpatterns-recipes-files-liquidingredientsinsoup-xq.html">xmlpatterns/recipes/files/liquidIngredientsInSoup.xq</a></li> <li><a href="xmlpatterns-recipes-files-mushroomsoup-xq.html">xmlpatterns/recipes/files/mushroomSoup.xq</a></li> <li><a href="xmlpatterns-recipes-files-preparationlessthan30-xq.html">xmlpatterns/recipes/files/preparationLessThan30.xq</a></li> <li><a href="xmlpatterns-recipes-files-preparationtimes-xq.html">xmlpatterns/recipes/files/preparationTimes.xq</a></li> <li><a href="xmlpatterns-recipes-forms-querywidget-ui.html">xmlpatterns/recipes/forms/querywidget.ui</a></li> <li><a href="xmlpatterns-recipes-forms-querywidget-mobiles-ui.html">xmlpatterns/recipes/forms/querywidget_mobiles.ui</a></li> <li><a href="xmlpatterns-recipes-main-cpp.html">xmlpatterns/recipes/main.cpp</a></li> <li><a href="xmlpatterns-recipes-recipes-pro.html">xmlpatterns/recipes/recipes.pro</a></li> <li><a href="xmlpatterns-recipes-recipes-qrc.html">xmlpatterns/recipes/recipes.qrc</a></li> </ul> <p>The Recipes example shows how to use <a href="qtxmlpatterns.html">QtXmlPatterns</a> to query XML data loaded from a file.<a name="introduction"></a> <h2>Introduction</h2> <p>In this case, the XML data represents a cookbook, <tt>cookbook.xml</tt>, which contains <tt><cookbook></tt> as its document element, which in turn contains a sequence of <tt><recipe></tt> elements. This XML data is searched using queries stored in <a href="xmlprocessing.html">XQuery</a> files (<tt>*.xq</tt>).</p> <a name="the-user-interface"></a> <h3>The User Interface</h3> <p>The UI for this example was created using <a href="designer-manual.html">Qt Designer</a>:</p> <p class="centerAlign"><img src="images/recipes-example.png" alt="" /></p><p>The UI consists of three <a href="qgroupbox.html">group boxes</a> arranged vertically. The top one contains a <a href="qtextedit.html">text viewer</a> that displays the XML text from the cookbook file. The middle group box contains a <a href="qcombobox.html">combo box</a> for choosing the <a href="xquery-introduction.html">XQuery</a> to run and a <a href="qtextedit.html">text viewer</a> for displaying the text of the selected <a href="xmlprocessing.html">XQuery</a>. The <tt>.xq</tt> files in the file list above are shown in the combo box menu. Choosing an <a href="xmlprocessing.html">XQuery</a> loads, parses, and runs the selected <a href="xmlprocessing.html">XQuery</a>. The query result is shown in the bottom group box's <a href="qtextedit.html">text viewer</a>.</p> <a name="running-your-own-xqueries"></a> <h3>Running your own XQueries</h3> <p>You can write your own <a href="xmlprocessing.html">XQuery</a> files and run them in the example program. The file <tt>xmlpatterns/recipes/recipes.qrc</tt> is the <a href="resources.html">resource file</a> for this example. It is used in <tt>main.cpp</tt> (<tt>Q_INIT_RESOURCE(recipes);</tt>). It lists the <a href="xmlprocessing.html">XQuery</a> files (<tt>.xq</tt>) that can be selected in the combobox.</p> <pre class="qml"> <!DOCTYPE RCC><RCC version="1.0"> <qresource> <file>files/cookbook.xml</file> <file>files/allRecipes.xq</file> <file>files/liquidIngredientsInSoup.xq</file> <file>files/mushroomSoup.xq</file> <file>files/preparationLessThan30.xq</file> <file>files/preparationTimes.xq</file> </qresource> </RCC></pre> <p>To add your own queries to the example's combobox, store your <tt>.xq</tt> files in the <tt>examples/xmlpatterns/recipes/files</tt> directory and add them to <tt>recipes.qrc</tt> as shown above.</p> <a name="code-walk-through"></a> <h2>Code Walk-Through</h2> <p>The example's main() function creates the standard instance of <a href="qapplication.html">QApplication</a>. Then it creates an instance of the UI class, shows it, and starts the Qt event loop:</p> <pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span><span class="operator">*</span> argv<span class="operator">[</span><span class="operator">]</span>) { Q_INIT_RESOURCE(recipes); <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv); QueryMainWindow<span class="operator">*</span> <span class="keyword">const</span> queryWindow <span class="operator">=</span> <span class="keyword">new</span> QueryMainWindow; <span class="preprocessor">#ifdef Q_OS_SYMBIAN</span> queryWindow<span class="operator">-</span><span class="operator">></span>showMaximized(); <span class="preprocessor">#else</span> queryWindow<span class="operator">-</span><span class="operator">></span>show(); <span class="preprocessor">#endif</span> <span class="keyword">return</span> app<span class="operator">.</span>exec(); }</pre> <a name="the-ui-class-querymainwindow"></a> <h3>The UI Class: QueryMainWindow</h3> <p>The example's UI is a conventional Qt GUI application inheriting <a href="qmainwindow.html">QMainWindow</a> and the class generated by <a href="designer-manual.html">Qt Designer</a>:</p> <pre class="cpp"> <span class="keyword">class</span> QueryMainWindow : <span class="keyword">public</span> <span class="type"><a href="qmainwindow.html">QMainWindow</a></span><span class="operator">,</span> <span class="keyword">private</span> Ui<span class="operator">::</span>QueryWidget { Q_OBJECT <span class="keyword">public</span>: QueryMainWindow(); <span class="keyword">public</span> <span class="keyword">slots</span>: <span class="type">void</span> displayQuery(<span class="type">int</span> index); <span class="keyword">private</span>: <span class="type"><a href="qcombobox.html">QComboBox</a></span><span class="operator">*</span> ui_defaultQueries; <span class="type">void</span> evaluate(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>str); <span class="type">void</span> loadInputFile(); };</pre> <p>The constructor finds the window's <a href="qcombobox.html">combo box</a> child widget and connects its <a href="qcombobox.html#currentIndexChanged">currentIndexChanged()</a> signal to the window's <tt>displayQuery()</tt> slot. It then calls <tt>loadInputFile()</tt> to load <tt>cookbook.xml</tt> and display its contents in the top group box's <a href="qtextedit.html">text viewer</a> . Finally, it finds the <a href="xmlprocessing.html">XQuery</a> files (<tt>.xq</tt>) and adds each one to the <a href="qcombobox.html">combo box</a> menu.</p> <pre class="cpp"> QueryMainWindow<span class="operator">::</span>QueryMainWindow() : ui_defaultQueries(<span class="number">0</span>) { setupUi(<span class="keyword">this</span>); <span class="keyword">new</span> XmlSyntaxHighlighter(findChild<span class="operator"><</span><span class="type"><a href="qtextedit.html">QTextEdit</a></span><span class="operator">*</span><span class="operator">></span>(<span class="string">"inputTextEdit"</span>)<span class="operator">-</span><span class="operator">></span>document()); <span class="keyword">new</span> XmlSyntaxHighlighter(findChild<span class="operator"><</span><span class="type"><a href="qtextedit.html">QTextEdit</a></span><span class="operator">*</span><span class="operator">></span>(<span class="string">"outputTextEdit"</span>)<span class="operator">-</span><span class="operator">></span>document()); ui_defaultQueries <span class="operator">=</span> findChild<span class="operator"><</span><span class="type"><a href="qcombobox.html">QComboBox</a></span><span class="operator">*</span><span class="operator">></span>(<span class="string">"defaultQueries"</span>); <span class="type"><a href="qmetaobject.html">QMetaObject</a></span><span class="operator">::</span>connectSlotsByName(<span class="keyword">this</span>); connect(ui_defaultQueries<span class="operator">,</span> SIGNAL(currentIndexChanged(<span class="type">int</span>))<span class="operator">,</span> SLOT(displayQuery(<span class="type">int</span>))); loadInputFile(); <span class="keyword">const</span> <span class="type"><a href="qstringlist.html">QStringList</a></span> queries(<span class="type"><a href="qdir.html">QDir</a></span>(<span class="string">":/files/"</span><span class="operator">,</span> <span class="string">"*.xq"</span>)<span class="operator">.</span>entryList()); <span class="type">int</span> len <span class="operator">=</span> queries<span class="operator">.</span>count(); <span class="keyword">for</span>(<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator"><</span> len; <span class="operator">+</span><span class="operator">+</span>i) ui_defaultQueries<span class="operator">-</span><span class="operator">></span>addItem(queries<span class="operator">.</span>at(i)); }</pre> <p>The work is done in the <a href="#displayquery-slot">displayQuery()</a> slot and the <a href="#evaluate-function">evaluate()</a> function it calls. <a href="#displayquery-slot">displayQuery()</a> loads and displays the selected query file and passes the <a href="xmlprocessing.html">XQuery</a> text to <a href="#evaluate-function">evaluate()</a>.</p> <a name="displayquery-slot"></a><pre class="cpp"> <span class="type">void</span> QueryMainWindow<span class="operator">::</span>displayQuery(<span class="type">int</span> index) { <span class="type"><a href="qfile.html">QFile</a></span> queryFile(<span class="type"><a href="qstring.html">QString</a></span>(<span class="string">":files/"</span>) <span class="operator">+</span> ui_defaultQueries<span class="operator">-</span><span class="operator">></span>itemText(index)); queryFile<span class="operator">.</span>open(<span class="type"><a href="qiodevice.html">QIODevice</a></span><span class="operator">::</span>ReadOnly); <span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> query(<span class="type"><a href="qstring.html">QString</a></span><span class="operator">::</span>fromLatin1(queryFile<span class="operator">.</span>readAll())); findChild<span class="operator"><</span><span class="type"><a href="qtextedit.html">QTextEdit</a></span><span class="operator">*</span><span class="operator">></span>(<span class="string">"queryTextEdit"</span>)<span class="operator">-</span><span class="operator">></span>setPlainText(query); evaluate(query); }</pre> <p><a href="#evaluate-function">evaluate()</a> demonstrates the standard <a href="qtxmlpatterns.html">QtXmlPatterns</a> usage pattern. First, an instance of <a href="qxmlquery.html">QXmlQuery</a> is created (<tt>query</tt>). The <tt>query's</tt> <a href="qxmlquery.html#bindVariable">bindVariable()</a> function is then called to bind the <tt>cookbook.xml</tt> file to the <a href="xmlprocessing.html">XQuery</a> variable <tt>inputDocument</tt>. <i>After</i> the variable is bound, <a href="qxmlquery.html#setQuery">setQuery()</a> is called to pass the <a href="xmlprocessing.html">XQuery</a> text to the <tt>query</tt>.</p> <p><b>Note:</b> <a href="qxmlquery.html#setQuery">setQuery()</a> must be called <i>after</i> <a href="qxmlquery.html#bindVariable">bindVariable()</a>.</p> <p>Passing the <a href="xmlprocessing.html">XQuery</a> to <a href="qxmlquery.html#setQuery">setQuery()</a> causes <a href="qtxmlpatterns.html">QtXmlPatterns</a> to parse the <a href="xmlprocessing.html">XQuery</a>. <a href="qxmlquery.html#isValid">QXmlQuery::isValid</a>() is called to ensure that the <a href="xmlprocessing.html">XQuery</a> was correctly parsed.</p> <a name="evaluate-function"></a><pre class="cpp"> <span class="type">void</span> QueryMainWindow<span class="operator">::</span>evaluate(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>str) { <span class="type"><a href="qfile.html">QFile</a></span> sourceDocument; sourceDocument<span class="operator">.</span>setFileName(<span class="string">":/files/cookbook.xml"</span>); sourceDocument<span class="operator">.</span>open(<span class="type"><a href="qiodevice.html">QIODevice</a></span><span class="operator">::</span>ReadOnly); <span class="type"><a href="qbytearray.html">QByteArray</a></span> outArray; <span class="type"><a href="qbuffer.html">QBuffer</a></span> buffer(<span class="operator">&</span>outArray); buffer<span class="operator">.</span>open(<span class="type"><a href="qiodevice.html">QIODevice</a></span><span class="operator">::</span>ReadWrite); <span class="type"><a href="qxmlquery.html">QXmlQuery</a></span> query; query<span class="operator">.</span>bindVariable(<span class="string">"inputDocument"</span><span class="operator">,</span> <span class="operator">&</span>sourceDocument); query<span class="operator">.</span>setQuery(str); <span class="keyword">if</span> (<span class="operator">!</span>query<span class="operator">.</span>isValid()) <span class="keyword">return</span>; <span class="type"><a href="qxmlformatter.html">QXmlFormatter</a></span> formatter(query<span class="operator">,</span> <span class="operator">&</span>buffer); <span class="keyword">if</span> (<span class="operator">!</span>query<span class="operator">.</span>evaluateTo(<span class="operator">&</span>formatter)) <span class="keyword">return</span>; buffer<span class="operator">.</span>close(); findChild<span class="operator"><</span><span class="type"><a href="qtextedit.html">QTextEdit</a></span><span class="operator">*</span><span class="operator">></span>(<span class="string">"outputTextEdit"</span>)<span class="operator">-</span><span class="operator">></span>setPlainText(<span class="type"><a href="qstring.html">QString</a></span><span class="operator">::</span>fromUtf8(outArray<span class="operator">.</span>constData())); }</pre> <p>If the <a href="xmlprocessing.html">XQuery</a> is valid, an instance of <a href="qxmlformatter.html">QXmlFormatter</a> is created to format the query result as XML into a <a href="qbuffer.html">QBuffer</a>. To evaluate the <a href="xmlprocessing.html">XQuery</a>, an overload of <a href="qxmlquery.html#evaluateTo">evaluateTo()</a> is called that takes a <a href="qabstractxmlreceiver.html">QAbstractXmlReceiver</a> for its output (<a href="qxmlformatter.html">QXmlFormatter</a> inherits <a href="qabstractxmlreceiver.html">QAbstractXmlReceiver</a>). Finally, the formatted XML result is displayed in the UI's bottom text view.</p> <p><b>Note:</b> Each <a href="xmlprocessing.html">XQuery</a> <tt>.xq</tt> file must declare the <tt>$inputDocument</tt> variable to represent the <tt>cookbook.xml</tt> document:</p> <pre class="cpp"> (: All ingredients <span class="keyword">for</span> Mushroom Soup<span class="operator">.</span> :) declare variable $inputDocument external; doc($inputDocument)<span class="operator">/</span>cookbook<span class="operator">/</span>recipe<span class="operator">[</span>@xml:id <span class="operator">=</span> <span class="string">"MushroomSoup"</span><span class="operator">]</span><span class="operator">/</span>ingredient<span class="operator">/</span> <span class="operator"><</span>p<span class="operator">></span>{@name<span class="operator">,</span> @quantity}<span class="operator"><</span><span class="operator">/</span>p<span class="operator">></span></pre> <p><b>Note:</b> If you add add your own query.xq files, you must declare the <tt>$inputDocument</tt> and use it as shown above.</p> </div> <!-- @@@xmlpatterns/recipes --> </div> </div> </div> <div class="ft"> <span></span> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2013 Digia Plc and/or its subsidiaries. Documentation contributions included herein are the copyrights of their respective owners.</p> <br /> <p> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.</p> <p> Documentation sources may be obtained from <a href="http://www.qt-project.org"> www.qt-project.org</a>.</p> <br /> <p> Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. <a title="Privacy Policy" href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p> </div> <script src="scripts/functions.js" type="text/javascript"></script> </body> </html>