outliner-example.html

来自「QT 下载资料仅供参考」· HTML 代码 · 共 279 行

HTML
279
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/examples/xml/outliner/outliner.doc:5 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Outliner to show use of DOM</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: #ffffff; color: black; }--></style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#E5E5E5"><td valign=center> <a href="index.html"><font color="#004faf">Home</font></a> | <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a> | <a href="functions.html"><font color="#004faf">Functions</font></a></td><td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Outliner to show use of DOM</h1> <p> <p> This example presents a small outliner program to show the basic usage ofthe <a href="xml.html#dom">DOM classes</a>. The format of the outlinesis the OPML format as described in http://www.opml.org/spec.<p> This example shows how to load a DOM tree from an XML file and how totraverse it.<p> <hr><p> Sample XML file (todos.opml):<p> <pre>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;&lt;opml version="1.0"&gt;    &lt;head&gt;        &lt;title&gt;Todo List&lt;/title&gt;        &lt;dateCreated&gt;Tue, 31 Oct 2000 17:00:17 CET&lt;/dateCreated&gt;        &lt;dateModified&gt;Tue, 31 Oct 2000 17:00:17 CET&lt;/dateModified&gt;        &lt;ownerName&gt;Arthur Dent&lt;/ownerName&gt;        &lt;ownerEmail&gt;info@trolltech.com&lt;/ownerEmail&gt;    &lt;/head&gt;    &lt;body&gt;        &lt;outline text="Background"&gt;            &lt;outline text="This is an example todo list."/&gt;        &lt;/outline&gt;        &lt;outline text="Books to read"&gt;            &lt;outline text="Science Fiction"&gt;                &lt;outline text="Philip K. Dick"&gt;                    &lt;outline text="Do Androids Dream of Electical Sheep?"/&gt;                    &lt;outline text="The Three Stigmata of Palmer Eldritch"/&gt;                &lt;/outline&gt;                &lt;outline text="Robert A. Heinlein"&gt;                    &lt;outline text="Stranger in a Strange Land"/&gt;                &lt;/outline&gt;                &lt;outline text="Isaac Asimov"&gt;                    &lt;outline text="Foundation and Empire"/&gt;                &lt;/outline&gt;            &lt;/outline&gt;            &lt;outline text="Qt Books (in English)"&gt;                &lt;outline text="Dalheimer: Programming with Qt"/&gt;                &lt;outline text="Griffith: KDE 2/Qt Programming Bible"/&gt;                &lt;outline text="Hughes: Linux Rapid Application Development"/&gt;                &lt;outline text="Solin: <a href="qt.html">Qt</a> Programming in 24 hours"/&gt;                &lt;outline text="Ward: <a href="qt.html">Qt</a> 2 Programming for Linux and Windows 2000"/&gt;            &lt;/outline&gt;        &lt;/outline&gt;        &lt;outline text="Shopping list"&gt;            &lt;outline text="General"&gt;                &lt;outline text="Towel"/&gt;                &lt;outline text="Hair dryer"/&gt;                &lt;outline text="Underpants"/&gt;            &lt;/outline&gt;            &lt;outline text="For Sunday"&gt;                &lt;outline text="Beef"/&gt;                &lt;outline text="Rice"/&gt;                &lt;outline text="Carrots"/&gt;                &lt;outline text="Beans"/&gt;                &lt;outline text="Beer"/&gt;                &lt;outline text="Wine"/&gt;                &lt;outline text="Orange juice"/&gt;            &lt;/outline&gt;        &lt;/outline&gt;        &lt;outline text="Write a letter to Ford"&gt;        &lt;/outline&gt;    &lt;/body&gt;&lt;/opml&gt;</pre><p> <hr><p> Header file (outlinetree.h):<p> <pre>/****************************************************************************** $Id:  qt/outlinetree.h   3.0.5   edited Oct 12 2001 $**** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.**** This file is part of an example program for Qt.  This example** program may be used, distributed and modified without limitation.*******************************************************************************/#ifndef OUTLINETREE_H#define OUTLINETREE_H#include &lt;<a href="qlistview-h.html">qlistview.h</a>&gt;#include &lt;<a href="qdom-h.html">qdom.h</a>&gt;class OutlineTree : public <a href="qlistview.html">QListView</a>{    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>public:    OutlineTree( const <a href="qstring.html">QString</a> fileName, QWidget *parent = 0, const char *name = 0 );    ~OutlineTree();private:    <a href="qdomdocument.html">QDomDocument</a> domTree;    void getHeaderInformation( const <a href="qdomelement.html">QDomElement</a> &amp;header );    void buildTree( <a href="qlistviewitem.html">QListViewItem</a> *parentItem, const <a href="qdomelement.html">QDomElement</a> &amp;parentElement );};#endif // OUTLINETREE_H</pre><p> <hr><p> Implementation (outlinetree.cpp):<p> <pre>/****************************************************************************** $Id:  qt/outlinetree.cpp   3.0.5   edited Oct 12 2001 $**** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.**** This file is part of an example program for Qt.  This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include "outlinetree.h"#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;<a name="f521"></a>OutlineTree::OutlineTree( const <a href="qstring.html">QString</a> fileName, QWidget *parent, const char *name )    : <a href="qlistview.html">QListView</a>( parent, name ){    // div. configuration of the list view    <a href="qlistview.html#addColumn">addColumn</a>( "Outlines" );    <a href="qlistview.html#setSorting">setSorting</a>( -1 );    <a href="qlistview.html#setRootIsDecorated">setRootIsDecorated</a>( TRUE );    // read the XML file and create DOM tree    <a href="qfile.html">QFile</a> opmlFile( fileName );<a name="x1904"></a>    if ( !opmlFile.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> ) ) {<a name="x1906"></a>        QMessageBox::<a href="qmessagebox.html#critical">critical</a>( 0,                <a href="qobject.html#tr">tr</a>( "Critical Error" ),                <a href="qobject.html#tr">tr</a>( "Cannot open file %1" ).arg( fileName ) );        return;    }    if ( !domTree.setContent( &amp;opmlFile ) ) {        QMessageBox::<a href="qmessagebox.html#critical">critical</a>( 0,                <a href="qobject.html#tr">tr</a>( "Critical Error" ),                <a href="qobject.html#tr">tr</a>( "Parsing error for file %1" ).arg( fileName ) );<a name="x1903"></a>        opmlFile.<a href="qfile.html#close">close</a>();        return;    }    opmlFile.<a href="qfile.html#close">close</a>();    // get the header information from the DOM    <a href="qdomelement.html">QDomElement</a> root = domTree.documentElement();    <a href="qdomnode.html">QDomNode</a> node;<a name="x1896"></a>    node = root.<a href="qdomnode.html#firstChild">firstChild</a>();<a name="x1898"></a>    while ( !node.<a href="qdomnode.html#isNull">isNull</a>() ) {<a name="x1900"></a><a name="x1897"></a>        if ( node.<a href="qdomnode.html#isElement">isElement</a>() &amp;&amp; node.<a href="qdomnode.html#nodeName">nodeName</a>() == "head" ) {<a name="x1902"></a>            <a href="qdomelement.html">QDomElement</a> header = node.<a href="qdomnode.html#toElement">toElement</a>();            getHeaderInformation( header );            break;        }<a name="x1899"></a>        node = node.<a href="qdomnode.html#nextSibling">nextSibling</a>();    }    // create the tree view out of the DOM    node = root.<a href="qdomnode.html#firstChild">firstChild</a>();    while ( !node.<a href="qdomnode.html#isNull">isNull</a>() ) {        if ( node.<a href="qdomnode.html#isElement">isElement</a>() &amp;&amp; node.<a href="qdomnode.html#nodeName">nodeName</a>() == "body" ) {            <a href="qdomelement.html">QDomElement</a> body = node.<a href="qdomnode.html#toElement">toElement</a>();            buildTree( 0, body );            break;        }        node = node.<a href="qdomnode.html#nextSibling">nextSibling</a>();    }}OutlineTree::~OutlineTree(){}void <a name="f522"></a>OutlineTree::getHeaderInformation( const <a href="qdomelement.html">QDomElement</a> &amp;header ){    // visit all children of the header element and look if you can make    // something with it    <a href="qdomnode.html">QDomNode</a> node = header.<a href="qdomnode.html#firstChild">firstChild</a>();    while ( !node.<a href="qdomnode.html#isNull">isNull</a>() ) {        if ( node.<a href="qdomnode.html#isElement">isElement</a>() ) {            // case for the different header entries            if ( node.<a href="qdomnode.html#nodeName">nodeName</a>() == "title" ) {                <a href="qdomtext.html">QDomText</a> textChild = node.<a href="qdomnode.html#firstChild">firstChild</a>().toText();                if ( !textChild.<a href="qdomnode.html#isNull">isNull</a>() ) {<a name="x1901"></a>                    <a href="qlistview.html#setColumnText">setColumnText</a>( 0, textChild.<a href="qdomnode.html#nodeValue">nodeValue</a>() );                }            }        }        node = node.<a href="qdomnode.html#nextSibling">nextSibling</a>();    }}void <a name="f523"></a>OutlineTree::buildTree( <a href="qlistviewitem.html">QListViewItem</a> *parentItem, const <a href="qdomelement.html">QDomElement</a> &amp;parentElement ){    <a href="qlistviewitem.html">QListViewItem</a> *thisItem = 0;    <a href="qdomnode.html">QDomNode</a> node = parentElement.<a href="qdomnode.html#firstChild">firstChild</a>();    while ( !node.<a href="qdomnode.html#isNull">isNull</a>() ) {        if ( node.<a href="qdomnode.html#isElement">isElement</a>() &amp;&amp; node.<a href="qdomnode.html#nodeName">nodeName</a>() == "outline" ) {            // add a new list view item for the outline            if ( parentItem == 0 )                thisItem = new <a href="qlistviewitem.html">QListViewItem</a>( this, thisItem );            else                thisItem = new <a href="qlistviewitem.html">QListViewItem</a>( parentItem, thisItem );<a name="x1905"></a>            thisItem-&gt;<a href="qlistviewitem.html#setText">setText</a>( 0, node.<a href="qdomnode.html#toElement">toElement</a>().attribute( "text" ) );            // recursive build of the tree            buildTree( thisItem, node.<a href="qdomnode.html#toElement">toElement</a>() );        }        node = node.<a href="qdomnode.html#nextSibling">nextSibling</a>();    }}</pre><p> <hr><p> Main (main.cpp):<p> <pre>/****************************************************************************** $Id:  qt/main.cpp   3.0.5   edited Oct 12 2001 $**** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.**** This file is part of an example program for Qt.  This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;#include "outlinetree.h"int main( int argc, char **argv ){    <a href="qapplication.html">QApplication</a> a( argc, argv );    OutlineTree outline( "todos.opml" );<a name="x1908"></a>    a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;outline );<a name="x1909"></a>    outline.<a href="qwidget.html#show">show</a>();<a name="x1907"></a>    return a.<a href="qapplication.html#exec">exec</a>();}</pre><p>See also <a href="xml-examples.html">Qt XML Examples</a>.<!-- eof --><p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright &copy; 2002 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a><td align=right><div align=right>Qt version 3.0.5</div></table></div></address></body></html>

⌨️ 快捷键说明

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