QDomAttr Class
QDomAttr 类表示QDomElement 的一个属性。更多内容...
| 头文件: | #include <QDomAttr> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Xml) target_link_libraries(mytarget PRIVATE Qt6::Xml) |
| qmake: | QT += xml |
| 继承自: | QDomNode |
- 所有成员列表(包括继承的成员)
- QDomAttr 属于XML 类。
注意:本类中的所有函数均为可重入的。
公共函数
| QDomAttr() | |
| QDomAttr(const QDomAttr &attr) | |
| QString | name() const |
| QDomNode::NodeType | nodeType() const |
| QDomElement | ownerElement() const |
| void | setValue(const QString &value) |
| bool | specified() const |
| QString | value() const |
| QDomAttr & | operator=(const QDomAttr &other) |
详细说明
例如,以下这段 XML 代码将生成一个没有子元素但具有两个属性的元素:
<link href="http://qt-project.org" color="red" />您可以通过如下代码访问元素的属性:
QDomElement e;
//...
QDomAttr a = e.attributeNode("href");
cout << qPrintable(a.value()) << '\n'; // prints "http://qt-project.org"
a.setValue("http://qt-project.org/doc"); // change the node's attribute
QDomAttr a2 = e.attributeNode("href");
cout << qPrintable(a2.value()) << '\n'; // prints "http://qt-project.org/doc"此示例还表明,修改从元素获取的属性会改变该元素本身的属性。若不希望更改元素属性的值,必须使用cloneNode() 来获取该属性的独立副本。
QDomAttr 可以返回属性的name() 和value()。属性的值可通过setValue() 进行设置。如果specified() 返回 true,则该值是通过setValue() 设置的。该属性所关联的节点(如有)可通过ownerElement() 获取。
有关文档对象模型(DOM)的更多信息,请参阅http://www.w3.org/TR/REC-DOM-Level-1/和http://www.w3.org/TR/DOM-Level-2-Core/。有关 DOM 实现的更通用的介绍,请参阅QDomDocument 文档。
成员函数文档
QDomAttr::QDomAttr()
创建一个空属性。
QDomAttr::QDomAttr(const QDomAttr &attr)
创建attr 的副本。
该副本的数据是共享的(浅拷贝):修改其中一个节点也会改变另一个节点。若要进行深拷贝,请使用cloneNode()。
QString QDomAttr::name() const
返回该属性的名称。
QDomNode::NodeType QDomAttr::nodeType() const
返回AttributeNode 。
QDomElement QDomAttr::ownerElement() const
返回该属性所关联的元素节点;如果该属性未关联到任何元素,则返回null node 。
void QDomAttr::setValue(const QString &value)
将该属性的值设置为value 。
另请参阅 value()。
bool QDomAttr::specified() const
如果该属性已被用户通过setValue() 设置,则返回true ;如果未指定或设置该值,则返回false 。
另请参阅 setValue()。
QString QDomAttr::value() const
返回该属性的值;如果未指定该属性,则返回空字符串。
QDomAttr &QDomAttr::operator=(const QDomAttr &other)
将other 赋值给此DOM属性。
复制的数据是共享的(浅拷贝):修改一个节点也会改变另一个节点。若要进行深拷贝,请使用 `cloneNode()`。
© 2026 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.