querymainwindow.cpp Example File | Qt 4.8 (original) (raw)
xmlpatterns/recipes/querymainwindow.cpp
#include #include
#include "querymainwindow.h" #include "xmlsyntaxhighlighter.h"
QueryMainWindow::QueryMainWindow() : ui_defaultQueries(0) { setupUi(this);
new XmlSyntaxHighlighter(findChild<[QTextEdit](qtextedit.html)*>("inputTextEdit")->document());
new XmlSyntaxHighlighter(findChild<[QTextEdit](qtextedit.html)*>("outputTextEdit")->document());
ui_defaultQueries = findChild<[QComboBox](qcombobox.html)*>("defaultQueries");
[QMetaObject](qmetaobject.html)::connectSlotsByName(this);
connect(ui_defaultQueries, SIGNAL(currentIndexChanged(int)), SLOT(displayQuery(int)));
loadInputFile();
const [QStringList](qstringlist.html) queries([QDir](qdir.html)(":/files/", "*.xq").entryList());
int len = queries.count();
for(int i = 0; i < len; ++i)
ui_defaultQueries->addItem(queries.at(i));}
void QueryMainWindow::displayQuery(int index) { QFile queryFile(QString(":files/") + ui_defaultQueries->itemText(index)); queryFile.open(QIODevice::ReadOnly); const QString query(QString::fromLatin1(queryFile.readAll())); findChild<QTextEdit*>("queryTextEdit")->setPlainText(query);
evaluate(query);}
void QueryMainWindow::loadInputFile() { QFile forView; forView.setFileName(":/files/cookbook.xml"); if (!forView.open(QIODevice::ReadOnly)) { QMessageBox::information(this, tr("Unable to open file"), forView.errorString()); return; }
[QTextStream](qtextstream.html) in(&forView);
[QString](qstring.html) inputDocument = in.readAll();
findChild<[QTextEdit](qtextedit.html)*>("inputTextEdit")->setPlainText(inputDocument);}
void QueryMainWindow::evaluate(const QString &str) { QFile sourceDocument; sourceDocument.setFileName(":/files/cookbook.xml"); sourceDocument.open(QIODevice::ReadOnly);
[QByteArray](qbytearray.html) outArray;
[QBuffer](qbuffer.html) buffer(&outArray);
buffer.open([QIODevice](qiodevice.html)::ReadWrite);
[QXmlQuery](qxmlquery.html) query;
query.bindVariable("inputDocument", &sourceDocument);
query.setQuery(str);
if (!query.isValid())
return;
[QXmlFormatter](qxmlformatter.html) formatter(query, &buffer);
if (!query.evaluateTo(&formatter))
return;
buffer.close();
findChild<[QTextEdit](qtextedit.html)*>("outputTextEdit")->setPlainText([QString](qstring.html)::fromUtf8(outArray.constData()));}
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.