先看下程序运行的结果图
跟使用QXmlStreamReader QXmlStreamWriter是一样的,区别在于处理的过程。
前一个例子主要是传参的方式,将QTreeWidget传给xmlReader,xmlWriter
读取xml到QTreeWidget里,随着读入添加item到里面,写的时候则遍历这个QTreeWidget,把item写入到xml文件
这个例子则是由QTreeWidget派生一个XbelTree的类出来,私有成员里有一个QDomDocument的对象。
算是两种处理方式吧,但传参和组合的方式每个例子都是可以用的,可能只是QXmlStream和QDom更适合哪种方式里了。
这个例子主要是学习QDom Classes
先贴一个Qt Assistant里的一段代码
The QDom classes are typically used as follows:
QDomDocument doc("mydocument");
QFile file("mydocument.xml");
if (!file.open(QIODevice::ReadOnly))
return;
if (!doc.setContent(&file)) {
file.close();
return;
}
file.close();
// print out the element names of all elements that are direct children
// of the outermost element.
QDomElement docElem = doc.documentElement();
QDomNode n = docElem.firstChild();
while(!n.isNull()) {
QDomElement e = n.toElement(); // try to convert the node to an element.
if(!e.isNull()) {
cout << qPrintable(e.tagName()) << endl; // the node really is an element.
}
n = n.nextSibling();
}
// Here we append a new element to the end of the document
QDomElement elem = doc.createElement("img");
elem.setAttribute("src", "myimage.png");
docElem.appendChild(elem);
Once doc and elem go out of scope, the whole internal tree representing the XML document is deleted.
To create a document using DOM use code like this:
QDomDocument doc("MyML");
QDomElement root = doc.createElement("MyML");
doc.appendChild(root);
QDomElement tag = doc.createElement("Greeting");
root.appendChild(tag);
QDomText t = doc.createTextNode("Hello World");
tag.appendChild(t);
QString xml = doc.toString();
加上这几句话产生xml文件查看内容
QFile file("mydocument.xml");
file.open(QIODevice::ReadWrite);
QTextStream m_out(&file);
doc.save(m_out,4);
file.close();
产生的document内容:
<!DOCTYPE MyML>
<MyML>
<Greeting>Hello Wolrd!</Greeting>
</MyML>
1.
XbelTree继承自QTreeWidget
每一列的大小是否可以自由改变,由该函数决定
void QHeaderView::setResizeMode ( ResizeMode mode )
Sets the constraints on how the header can be resized to those described by the given mode.
The resize mode specifies the behavior of the header sections. It can be set on the entire header view or on individual sections using setResizeMode().
Constant Value Description
QHeaderView::Interactive 0 The user can resize the section. The section can also be resized programmatically using resizeSection(). The section size defaults to defaultSectionSize. (See also cascadingSectionResizes.)
QHeaderView::Fixed 2 The user cannot resize the section. The section can only be resized programmatically using resizeSection(). The section size defaults to defaultSectionSize.
QHeaderView::Stretch 1 QHeaderView will automatically resize the section to fill the available space. The size cannot be changed by the user or programmatically.
QHeaderView::ResizeToContents 3 QHeaderView will automatically resize the section to its optimal size based on the contents of the entire column or row. The size cannot be changed by the user or programmatically. (This value was introduced in 4.2)
最上一排的headview的指针通过该函数获得
QHeaderView * QTreeView::header () const
Returns the header for the tree view.
2.
<img src="myimg.png">
tagName:img
name:src
value:"myimg.png"
QDom
A
QDomElement QDomDocument::documentElement () const
Returns the root element of the document.
该函数返回的即在DOCTYPE后最大的一个Element,即root
B
QDomElement QDomNode::firstChildElement ( const QString & tagName = QString() ) const
Returns the first child element with tag name tagName if tagName is non-empty; otherwise returns the first child element. Returns a null element if no such child exists.
该函数返回指定tagName的QDomElement
相关的有
firstChildElement(),lastChildElement(), previousSiblingElement(), and nextSiblingElement().
都需要指定tagName
当子结点的不是一个Element时,使用函数获得第一个子节点
QDomNode QDomNode::firstChild () const
Returns the first child of the node. If there is no child node, a null node is returned. Changing the returned node will also change the node in the document tree.
获得最后一个子结点
QDomNode QDomNode::lastChild () const
Returns the last child of the node. If there is no child node, a null node is returned. Changing the returned node will also change the node in the document tree.
See also firstChild() and childNodes().
获得下一个子节点
QDomNode QDomNode::nextSibling () const
Returns the next sibling in the document tree. Changing the returned node will also change the node in the document tree.
If you have XML like this:
<h1>Heading</h1>
<p>The text...</p>
<h2>Next heading</h2>
and this QDomNode represents the <p> tag, nextSibling() will return the node representing the <h2> tag.
获得上一个子节点
QDomNode QDomNode::previousSibling () const
Returns the previous sibling in the document tree. Changing the returned node will also change the node in the document tree.
For example, if you have XML like this:
<h1>Heading</h1>
<p>The text...</p>
<h2>Next heading</h2>
and this QDomNode represents the <p> tag, previousSibling() will return the node representing the <h1> tag.
你也可以直接获得结点的列表
QDomNodeList QDomNode::childNodes () const
Returns a list of all direct child nodes.
Most often you will call this function on a QDomElement object.
For example, if the XML document looks like this:
<body>
<h1>Heading</h1>
<p>Hello <b>you</b></p>
</body>
Then the list of child nodes for the "body"-element will contain the node created by the <h1> tag and the node created by the <p> tag.
The nodes in the list are not copied; so changing the nodes in the list will also change the children of this node.