saxhandler.cpp

来自「c++ GUI Programming with QT4书中的源码」· C++ 代码 · 共 63 行

CPP
63
字号
#include <QtGui>#include "saxhandler.h"SaxHandler::SaxHandler(QTreeWidget *tree){    treeWidget = tree;    currentItem = 0;}bool SaxHandler::startElement(const QString & /* namespaceURI */,                              const QString & /* localName */,                              const QString &qName,                              const QXmlAttributes &attributes){    if (qName == "entry") {        if (currentItem) {            currentItem = new QTreeWidgetItem(currentItem);        } else {            currentItem = new QTreeWidgetItem(treeWidget);        }        currentItem->setText(0, attributes.value("term"));    } else if (qName == "page") {        currentText.clear();    }    return true;}bool SaxHandler::characters(const QString &str){    currentText += str;    return true;}bool SaxHandler::endElement(const QString & /* namespaceURI */,                            const QString & /* localName */,                            const QString &qName){    if (qName == "entry") {        currentItem = currentItem->parent();    } else if (qName == "page") {        if (currentItem) {            QString allPages = currentItem->text(1);            if (!allPages.isEmpty())                allPages += ", ";            allPages += currentText;            currentItem->setText(1, allPages);        }    }    return true;}bool SaxHandler::fatalError(const QXmlParseException &exception){    QMessageBox::warning(0, QObject::tr("SAX Handler"),                         QObject::tr("Parse error at line %1, column "                                     "%2:\n%3.")                         .arg(exception.lineNumber())                         .arg(exception.columnNumber())                         .arg(exception.message()));    return false;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?