IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Recipes Example

Using Qt XML Patterns to query XML data loaded from a file.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

Recipes Example▲

The Recipes example shows how to use Qt XML Patterns to query XML data loaded from a file.

Introduction▲

In this case, the XML data represents a cookbook, cookbook.xml, which contains <cookbook> as its document element, which in turn contains a sequence of <recipe> elements. This XML data is searched using queries stored in XQuery files (*.xq).

The User Interface▲

The UI for this example was created using https://:

Image non disponible

The UI consists of three https:// arranged vertically. The top one contains a https:// that displays the XML text from the cookbook file. The middle group box contains a https:// for choosing the https:// to run and a https:// for displaying the text of the selected XQuery. The .xq files in the file list above are shown in the combo box menu. Choosing an XQuery loads, parses, and runs the selected XQuery. The query result is shown in the bottom group box's https://.

Running your own XQueries▲

You can write your own XQuery files and run them in the example program. The file xmlpatterns/recipes/recipes.qrc is the https:// for this example. It is used in main.cpp (Q_INIT_RESOURCE(recipes);). It lists the XQuery files (.xq) that can be selected in the combobox.

 
Sélectionnez
&lt;!DOCTYPE RCC&gt;&lt;RCC version="1.0"&gt;
&lt;qresource&gt;
    &lt;file&gt;files/cookbook.xml&lt;/file&gt;
    &lt;file&gt;files/allRecipes.xq&lt;/file&gt;
    &lt;file&gt;files/liquidIngredientsInSoup.xq&lt;/file&gt;
    &lt;file&gt;files/mushroomSoup.xq&lt;/file&gt;
    &lt;file&gt;files/preparationLessThan30.xq&lt;/file&gt;
    &lt;file&gt;files/preparationTimes.xq&lt;/file&gt;
&lt;/qresource&gt;
&lt;/RCC&gt;

To add your own queries to the example's combobox, store your .xq files in the examples/xmlpatterns/recipes/files directory and add them to recipes.qrc as shown above.

Code Walk-Through▲

The example's main() function creates the standard instance of https://. Then it creates an instance of the UI class, shows it, and starts the Qt event loop:

 
Sélectionnez
int main(int argc, char* argv[])
{
    Q_INIT_RESOURCE(recipes);
    QApplication app(argc, argv);
    QueryMainWindow* const queryWindow = new QueryMainWindow;
    queryWindow-&gt;show();
    return app.exec();
}

The UI Class: QueryMainWindow▲

The example's UI is a conventional Qt GUI application inheriting https:// and the class generated by https://:

 
Sélectionnez
class QueryMainWindow : public QMainWindow,
                        private Ui::QueryWidget
{
    Q_OBJECT

public:
    QueryMainWindow();

public slots:
    void displayQuery(int index);

private:
    QComboBox *ui_defaultQueries = nullptr;

    void evaluate(const QString &amp;str);
    void loadInputFile();
};

The constructor finds the window's https:// child widget and connects its https://() signal to the window's displayQuery() slot. It then calls loadInputFile() to load cookbook.xml and display its contents in the top group box's https:// . Finally, it finds the XQuery files (.xq) and adds each one to the https:// menu.

 
Sélectionnez
QueryMainWindow::QueryMainWindow()
{
    setupUi(this);

    new XmlSyntaxHighlighter(findChild&lt;QTextEdit*&gt;("inputTextEdit")-&gt;document());
    new XmlSyntaxHighlighter(findChild&lt;QTextEdit*&gt;("outputTextEdit")-&gt;document());

    ui_defaultQueries = findChild&lt;QComboBox*&gt;("defaultQueries");
    QMetaObject::connectSlotsByName(this);

    connect(ui_defaultQueries, QOverload&lt;int&gt;::of(&amp;QComboBox::currentIndexChanged),
            this, &amp;QueryMainWindow::displayQuery);

    loadInputFile();
    const QStringList queries(QDir(":/files/", "*.xq").entryList());
    for (const auto &amp;query : queries)
        ui_defaultQueries-&gt;addItem(query);
    if (queries.count() &gt; 0)
        displayQuery(0);
}

The work is done in the https:// slot and the https:// function it calls. https:// loads and displays the selected query file and passes the XQuery text to https://.

 
Sélectionnez
void QueryMainWindow::displayQuery(int index)
{
    QFile queryFile(QString(":files/") + ui_defaultQueries-&gt;itemText(index));
    queryFile.open(QIODevice::ReadOnly);
    const QString query(QString::fromLatin1(queryFile.readAll()));
    findChild&lt;QTextEdit*&gt;("queryTextEdit")-&gt;setPlainText(query);

    evaluate(query);
}

https:// demonstrates the standard Qt XML Patterns usage pattern. First, an instance of https:// is created (query). The query's https://() function is then called to bind the cookbook.xml file to the XQuery variable inputDocument. After the variable is bound, https://() is called to pass the XQuery text to the query.

https://() must be called after https://().

Passing the XQuery to https://() causes Qt XML Patterns to parse the XQuery. https://() is called to ensure that the XQuery was correctly parsed.

 
Sélectionnez
void QueryMainWindow::evaluate(const QString &amp;str)
{
    QFile sourceDocument;
    sourceDocument.setFileName(":/files/cookbook.xml");
    sourceDocument.open(QIODevice::ReadOnly);

    QByteArray outArray;
    QBuffer buffer(&amp;outArray);
    buffer.open(QIODevice::ReadWrite);

    QXmlQuery query;
    query.bindVariable("inputDocument", &amp;sourceDocument);
    query.setQuery(str);
    if (!query.isValid())
        return;

    QXmlFormatter formatter(query, &amp;buffer);
    if (!query.evaluateTo(&amp;formatter))
        return;

    buffer.close();
    findChild&lt;QTextEdit*&gt;("outputTextEdit")-&gt;setPlainText(QString::fromUtf8(outArray.constData()));

}

If the XQuery is valid, an instance of https:// is created to format the query result as XML into a https://. To evaluate the XQuery, an overload of https://() is called that takes a https:// for its output (https:// inherits https://). Finally, the formatted XML result is displayed in the UI's bottom text view.

Each XQuery .xq file must declare the $inputDocument variable to represent the cookbook.xml document:

 
Sélectionnez
(: All ingredients for Mushroom Soup. :)
declare variable $inputDocument external;

doc($inputDocument)/cookbook/recipe[@xml:id = "MushroomSoup"]/ingredient/
&lt;p&gt;{@name, @quantity}&lt;/p&gt;

If you add add your own query.xq files, you must declare the $inputDocument and use it as shown above.

List of Files▲

Files:

List of Files▲

  • recipes/files/allRecipes.xq

  • https://

  • recipes/files/liquidIngredientsInSoup.xq

  • recipes/files/mushroomSoup.xq

  • recipes/files/preparationLessThan30.xq

  • recipes/files/preparationTimes.xq

  • recipes/forms/querywidget.ui

  • recipes/main.cpp

  • recipes/querymainwindow.cpp

  • recipes/querymainwindow.h

  • recipes/recipes.pro

  • recipes/recipes.qrc

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Ce document est disponible sous la licence https:// telle que publiée par la Free Software Foundation.