diff options
author | Thorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com> | 2009-08-19 13:06:15 (GMT) |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com> | 2009-08-19 13:56:09 (GMT) |
commit | 0d9cc367b72d74d709b9a03748db6dd48e126472 (patch) | |
tree | 2cbf41b830c4e14c7454986d49fe2a73b6bb72ad /examples/xml/streambookmarks/xbelreader.cpp | |
parent | 332ca73378043970087f843036d02192e3fef50d (diff) | |
download | Qt-0d9cc367b72d74d709b9a03748db6dd48e126472.zip Qt-0d9cc367b72d74d709b9a03748db6dd48e126472.tar.gz Qt-0d9cc367b72d74d709b9a03748db6dd48e126472.tar.bz2 |
Changed the streambookmarks example to use aggregation
QXmlStreamReader and QXmlStreamWriter can be used conveniently without
subclassing, which the example now demonstrates.
Reviewed-by: mae
Reviewed-by: David Boddie
Diffstat (limited to 'examples/xml/streambookmarks/xbelreader.cpp')
-rw-r--r-- | examples/xml/streambookmarks/xbelreader.cpp | 68 |
1 files changed, 39 insertions, 29 deletions
diff --git a/examples/xml/streambookmarks/xbelreader.cpp b/examples/xml/streambookmarks/xbelreader.cpp index 99a7f34..0770643 100644 --- a/examples/xml/streambookmarks/xbelreader.cpp +++ b/examples/xml/streambookmarks/xbelreader.cpp @@ -60,33 +60,43 @@ XbelReader::XbelReader(QTreeWidget *treeWidget) //! [1] bool XbelReader::read(QIODevice *device) { - setDevice(device); + xml.setDevice(device); - if (readNextStartElement()) { - if (name() == "xbel" && attributes().value("version") == "1.0") + if (xml.readNextStartElement()) { + if (xml.name() == "xbel" && xml.attributes().value("version") == "1.0") readXBEL(); else - raiseError(QObject::tr("The file is not an XBEL version 1.0 file.")); + xml.raiseError(QObject::tr("The file is not an XBEL version 1.0 file.")); } - return !error(); + return !xml.error(); } //! [1] +//! [2] +QString XbelReader::errorString() const +{ + return QObject::tr("%1\nLine %2, column %3") + .arg(xml.errorString()) + .arg(xml.lineNumber()) + .arg(xml.columnNumber()); +} +//! [2] + //! [3] void XbelReader::readXBEL() { - Q_ASSERT(isStartElement() && name() == "xbel"); + Q_ASSERT(xml.isStartElement() && xml.name() == "xbel"); - while (readNextStartElement()) { - if (name() == "folder") + while (xml.readNextStartElement()) { + if (xml.name() == "folder") readFolder(0); - else if (name() == "bookmark") + else if (xml.name() == "bookmark") readBookmark(0); - else if (name() == "separator") + else if (xml.name() == "separator") readSeparator(0); else - skipCurrentElement(); + xml.skipCurrentElement(); } } //! [3] @@ -94,9 +104,9 @@ void XbelReader::readXBEL() //! [4] void XbelReader::readTitle(QTreeWidgetItem *item) { - Q_ASSERT(isStartElement() && name() == "title"); + Q_ASSERT(xml.isStartElement() && xml.name() == "title"); - QString title = readElementText(); + QString title = xml.readElementText(); item->setText(0, title); } //! [4] @@ -104,52 +114,52 @@ void XbelReader::readTitle(QTreeWidgetItem *item) //! [5] void XbelReader::readSeparator(QTreeWidgetItem *item) { - Q_ASSERT(isStartElement() && name() == "separator"); + Q_ASSERT(xml.isStartElement() && xml.name() == "separator"); QTreeWidgetItem *separator = createChildItem(item); separator->setFlags(item->flags() & ~Qt::ItemIsSelectable); separator->setText(0, QString(30, 0xB7)); - skipCurrentElement(); + xml.skipCurrentElement(); } //! [5] void XbelReader::readFolder(QTreeWidgetItem *item) { - Q_ASSERT(isStartElement() && name() == "folder"); + Q_ASSERT(xml.isStartElement() && xml.name() == "folder"); QTreeWidgetItem *folder = createChildItem(item); - bool folded = (attributes().value("folded") != "no"); + bool folded = (xml.attributes().value("folded") != "no"); treeWidget->setItemExpanded(folder, !folded); - while (readNextStartElement()) { - if (name() == "title") + while (xml.readNextStartElement()) { + if (xml.name() == "title") readTitle(folder); - else if (name() == "folder") + else if (xml.name() == "folder") readFolder(folder); - else if (name() == "bookmark") + else if (xml.name() == "bookmark") readBookmark(folder); - else if (name() == "separator") + else if (xml.name() == "separator") readSeparator(folder); else - skipCurrentElement(); + xml.skipCurrentElement(); } } void XbelReader::readBookmark(QTreeWidgetItem *item) { - Q_ASSERT(isStartElement() && name() == "bookmark"); + Q_ASSERT(xml.isStartElement() && xml.name() == "bookmark"); QTreeWidgetItem *bookmark = createChildItem(item); bookmark->setFlags(bookmark->flags() | Qt::ItemIsEditable); bookmark->setIcon(0, bookmarkIcon); bookmark->setText(0, QObject::tr("Unknown title")); - bookmark->setText(1, attributes().value("href").toString()); + bookmark->setText(1, xml.attributes().value("href").toString()); - while (readNextStartElement()) { - if (name() == "title") + while (xml.readNextStartElement()) { + if (xml.name() == "title") readTitle(bookmark); else - skipCurrentElement(); + xml.skipCurrentElement(); } } @@ -161,6 +171,6 @@ QTreeWidgetItem *XbelReader::createChildItem(QTreeWidgetItem *item) } else { childItem = new QTreeWidgetItem(treeWidget); } - childItem->setData(0, Qt::UserRole, name().toString()); + childItem->setData(0, Qt::UserRole, xml.name().toString()); return childItem; } |