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()
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() 返回。

有关文档对象模型的更多信息,请参阅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

返回属性值,如果未指定属性,则返回空字符串。

另请参阅 specified() 和setValue()。

QDomAttr &QDomAttr::operator=(const QDomAttr &other)

为该 DOM 属性指定other

副本的数据是共享的(浅层副本):修改一个节点也会改变另一个节点。如果要进行深度复制,请使用cloneNode()。

© 2025 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.