QWebElement Class Reference

#include <mw/QtWebKit/qwebelement.h>

class QWebElement

The QWebElement class provides convenient access to DOM elements in a QWebFrame. QtWebKit.

Public Member Enumerations
enumStyleResolveStrategy { InlineStyle, CascadedStyle, ComputedStyle }
Public Member Functions
QWebElement()
QWebElement(const QWebElement &)
~QWebElement()
voidaddClass(const QString &)
voidappendInside(const QString &)
voidappendInside(const QWebElement &)
voidappendOutside(const QString &)
voidappendOutside(const QWebElement &)
QString attribute(const QString &, const QString &)
QString attributeNS(const QString &, const QString &, const QString &)
QStringList attributeNames(const QString &)
QStringList classes()
QWebElement clone()
QWebElement document()
voidencloseContentsWith(const QWebElement &)
voidencloseContentsWith(const QString &)
voidencloseWith(const QString &)
voidencloseWith(const QWebElement &)
QVariant evaluateJavaScript(const QString &)
QWebElementCollection findAll(const QString &)
QWebElement findFirst(const QString &)
QWebElement firstChild()
QRectgeometry()
boolhasAttribute(const QString &)
boolhasAttributeNS(const QString &, const QString &)
boolhasAttributes()
boolhasClass(const QString &)
boolhasFocus()
boolisNull()
QWebElement lastChild()
QString localName()
QString namespaceUri()
QWebElement nextSibling()
booloperator!=(const QWebElement &)
QWebElement &operator=(const QWebElement &)
booloperator==(const QWebElement &)
QWebElement parent()
QString prefix()
voidprependInside(const QString &)
voidprependInside(const QWebElement &)
voidprependOutside(const QString &)
voidprependOutside(const QWebElement &)
QWebElement previousSibling()
voidremoveAllChildren()
voidremoveAttribute(const QString &)
voidremoveAttributeNS(const QString &, const QString &)
voidremoveClass(const QString &)
voidremoveFromDocument()
voidrender(QPainter *)
voidreplace(const QString &)
voidreplace(const QWebElement &)
voidsetAttribute(const QString &, const QString &)
voidsetAttributeNS(const QString &, const QString &, const QString &)
voidsetFocus()
voidsetInnerXml(const QString &)
voidsetOuterXml(const QString &)
voidsetPlainText(const QString &)
voidsetStyleProperty(const QString &, const QString &)
QString styleProperty(const QString &, StyleResolveStrategy)
QString tagName()
QWebElement &takeFromDocument()
QString toInnerXml()
QString toOuterXml()
QString toPlainText()
voidtoggleClass(const QString &)
QWebFrame *webFrame()

Detailed Description

Since
4.6 A QWebElement object allows easy access to the document model, represented by a tree-like structure of DOM elements. The root of the tree is called the document element and can be accessed using QWebFrame::documentElement().
Specific elements can be accessed using findAll() and findFirst(). These elements are identified using CSS selectors. The code snippet below demonstrates the use of findAll().

The first list contains all span elements in the document. The second list contains span elements that are children of p, classified with intro.

Using findFirst() is more efficient than calling findAll(), and extracting the first element only in the list returned.

Alternatively you can traverse the document manually using firstChild() and nextSibling():

Individual elements can be inspected or changed using methods such as attribute() or setAttribute(). For examle, to capture the user's input in a text field for later use (auto-completion), a browser could do something like this:

When the same page is later revisited, the browser can fill in the text field automatically by modifying the value attribute of the input element:

Another use case is to emulate a click event on an element. The following code snippet demonstrates how to call the JavaScript DOM method click() of a submit button:

The underlying content of QWebElement is explicitly shared. Creating a copy of a QWebElement does not create a copy of the content. Instead, both instances point to the same element.

The contents of child elements can be converted to plain text with toPlainText(); to XHTML using toInnerXml(). To include the element's tag in the output, use toOuterXml().

It is possible to replace the contents of child elements using setPlainText() and setInnerXml(). To replace the element itself and its contents, use setOuterXml().

Member Enumeration Documentation

Enum StyleResolveStrategy

This enum describes how QWebElement's styleProperty resolves the given property name.

InlineStyle Return the property value as it is defined in the element, without respecting style inheritance and other CSS rules. CascadedStyle The property's value is determined using the inheritance and importance rules defined in the document's stylesheet. ComputedStyle The property's value is the absolute value of the style property resolved from the environment.

EnumeratorValueDescription
InlineStyle
CascadedStyle
ComputedStyle

Constructor & Destructor Documentation

QWebElement ( )

QWebElement()

Constructs a null web element.

QWebElement ( const QWebElement & )

QWebElement(const QWebElement &)

Constructs a copy of other.

~QWebElement ( )

~QWebElement()

Destroys the element. However, the underlying DOM element is not destroyed.

Member Function Documentation

addClass ( const QString & )

voidaddClass(const QString &name)

Adds the specified class with the given name to the element.

appendInside ( const QString & )

voidappendInside(const QString &markup)

Appends the result of parsing markup as the element's last child.

Calling this function on a null element does nothing.

See also: prependInside(), prependOutside(), appendOutside()

appendInside ( const QWebElement & )

voidappendInside(const QWebElement &element)

Appends the given element as the element's last child.

If element is the child of another element, it is re-parented to this element. If element is a child of this element, then its position in the list of children is changed.

Calling this function on a null element does nothing.

See also: prependInside(), prependOutside(), appendOutside()

appendOutside ( const QString & )

voidappendOutside(const QString &markup)

Inserts the result of parsing markup after this element.

Calling this function on a null element does nothing.

See also: appendInside(), prependInside(), prependOutside()

appendOutside ( const QWebElement & )

voidappendOutside(const QWebElement &element)

Inserts the given element after this element.

If element is the child of another element, it is re-parented to the parent of this element.

Calling this function on a null element does nothing.

See also: appendInside(), prependInside(), prependOutside()

attribute ( const QString &, const QString & )

QString attribute(const QString &name,
const QString &defaultValue = QString()
)const

Returns the attribute with the given name. If the attribute does not exist, defaultValue is returned.

See also: setAttribute(), setAttributeNS(), attributeNS()

attributeNS ( const QString &, const QString &, const QString & )

QString attributeNS(const QString &namespaceUri,
const QString &name,
const QString &defaultValue = QString()
)const

Returns the attribute with the given name in namespaceUri. If the attribute does not exist, defaultValue is returned.

See also: setAttributeNS(), setAttribute(), attribute()

attributeNames ( const QString & )

QStringList attributeNames(const QString &namespaceUri = QString())const

Return the list of attributes for the namespace given as namespaceUri.

See also: attribute(), setAttribute()

classes ( )

QStringList classes()const

Returns the list of classes of this element.

clone ( )

QWebElement clone()const

Returns a clone of this element.

The clone may be inserted at any point in the document.

See also: appendInside(), prependInside(), prependOutside(), appendOutside()

document ( )

QWebElement document()const

Returns the document which this element belongs to.

encloseContentsWith ( const QWebElement & )

voidencloseContentsWith(const QWebElement &element)

Encloses the contents of this element with element. This element becomes the child of the deepest descendant within element.

### illustration

See also: encloseWith()

encloseContentsWith ( const QString & )

voidencloseContentsWith(const QString &markup)

Encloses the contents of this element with the result of parsing markup. This element becomes the child of the deepest descendant within markup.

See also: encloseWith()

encloseWith ( const QString & )

voidencloseWith(const QString &markup)

Encloses this element with the result of parsing markup. This element becomes the child of the deepest descendant within markup.

See also: replace()

encloseWith ( const QWebElement & )

voidencloseWith(const QWebElement &element)

Encloses this element with element. This element becomes the child of the deepest descendant within element.

See also: replace()

evaluateJavaScript ( const QString & )

QVariant evaluateJavaScript(const QString &scriptSource)

Executes scriptSource with this element as this object.

findAll ( const QString & )

QWebElementCollection findAll(const QString &selectorQuery)const

Returns a new list of child elements matching the given CSS selector selectorQuery. If there are no matching elements, an empty list is returned.

{Standard CSS2 selector} syntax is used for the query.

Note:

This search is performed recursively.

See also: findFirst()

findFirst ( const QString & )

QWebElement findFirst(const QString &selectorQuery)const

Returns the first child element that matches the given CSS selector selectorQuery.

{Standard CSS2 selector} syntax is used for the query.

Note:

This search is performed recursively.

See also: findAll()

firstChild ( )

QWebElement firstChild()const

Returns the element's first child.

See also: lastChild(), previousSibling(), nextSibling()

geometry ( )

QRectgeometry()const

Returns the geometry of this element, relative to its containing frame.

See also: tagName()

hasAttribute ( const QString & )

boolhasAttribute(const QString &name)const

Returns true if this element has an attribute with the given name; otherwise returns false.

See also: attribute(), setAttribute()

hasAttributeNS ( const QString &, const QString & )

boolhasAttributeNS(const QString &namespaceUri,
const QString &name
)const

Returns true if this element has an attribute with the given name, in namespaceUri; otherwise returns false.

See also: attributeNS(), setAttributeNS()

hasAttributes ( )

boolhasAttributes()const

Returns true if the element has any attributes defined; otherwise returns false;

See also: attribute(), setAttribute()

hasClass ( const QString & )

boolhasClass(const QString &name)const

Returns true if this element has a class with the given name; otherwise returns false.

hasFocus ( )

boolhasFocus()const

Returns true if the element has keyboard input focus; otherwise, returns false

See also: setFocus()

isNull ( )

boolisNull()const

Returns true if the element is a null element; otherwise returns false.

lastChild ( )

QWebElement lastChild()const

Returns the element's last child.

See also: firstChild(), previousSibling(), nextSibling()

localName ( )

QString localName()const

Returns the local name of the element. If the element does not use namespaces, an empty string is returned.

namespaceUri ( )

QString namespaceUri()const

Returns the namespace URI of this element. If the element has no namespace URI, an empty string is returned.

nextSibling ( )

QWebElement nextSibling()const

Returns the element's next sibling.

See also: firstChild(), previousSibling(), lastChild()

operator!= ( const QWebElement & )

booloperator!=(const QWebElement &o)const [inline]

Returns true if this element points to a different underlying DOM object than o; otherwise returns false.

operator= ( const QWebElement & )

QWebElement &operator=(const QWebElement &)

Assigns other to this element and returns a reference to this element.

operator== ( const QWebElement & )

booloperator==(const QWebElement &o)const [inline]

Returns true if this element points to the same underlying DOM object as o; otherwise returns false.

parent ( )

QWebElement parent()const

Returns the parent element of this elemen. If this element is the root document element, a null element is returned.

prefix ( )

QString prefix()const

Returns the namespace prefix of the element. If the element has no\ namespace prefix, empty string is returned.

prependInside ( const QString & )

voidprependInside(const QString &markup)

Prepends the result of parsing markup as the element's first child.

Calling this function on a null element does nothing.

See also: appendInside(), prependOutside(), appendOutside()

prependInside ( const QWebElement & )

voidprependInside(const QWebElement &element)

Prepends element as the element's first child.

If element is the child of another element, it is re-parented to this element. If element is a child of this element, then its position in the list of children is changed.

Calling this function on a null element does nothing.

See also: appendInside(), prependOutside(), appendOutside()

prependOutside ( const QString & )

voidprependOutside(const QString &markup)

Inserts the result of parsing markup before this element.

Calling this function on a null element does nothing.

See also: appendInside(), prependInside(), appendOutside()

prependOutside ( const QWebElement & )

voidprependOutside(const QWebElement &element)

Inserts the given element before this element.

If element is the child of another element, it is re-parented to the parent of this element.

Calling this function on a null element does nothing.

See also: appendInside(), prependInside(), appendOutside()

previousSibling ( )

QWebElement previousSibling()const

Returns the element's previous sibling.

See also: firstChild(), nextSibling(), lastChild()

removeAllChildren ( )

voidremoveAllChildren()

Removes all children from this element.

See also: removeFromDocument(), takeFromDocument()

removeAttribute ( const QString & )

voidremoveAttribute(const QString &name)

Removes the attribute with the given name from this element.

See also: attribute(), setAttribute(), hasAttribute()

removeAttributeNS ( const QString &, const QString & )

voidremoveAttributeNS(const QString &namespaceUri,
const QString &name
)

Removes the attribute with the given name, in namespaceUri, from this element.

See also: attributeNS(), setAttributeNS(), hasAttributeNS()

removeClass ( const QString & )

voidremoveClass(const QString &name)

Removes the specified class with the given name from the element.

removeFromDocument ( )

voidremoveFromDocument()

Removes this element from the document and makes it a null element.

See also: removeAllChildren(), takeFromDocument()

render ( QPainter * )

voidrender(QPainter *painter)

Render the element into painter .

replace ( const QString & )

voidreplace(const QString &markup)

Replaces this element with the result of parsing markup.

This method will not replace the <html>, <head> or <body> elements.

See also: encloseWith()

replace ( const QWebElement & )

voidreplace(const QWebElement &element)

Replaces this element with element.

This method will not replace the <html>, <head> or <body> elements.

See also: encloseWith()

setAttribute ( const QString &, const QString & )

voidsetAttribute(const QString &name,
const QString &value
)

Adds an attribute with the given name and value. If an attribute with the same name exists, its value is replaced by value.

See also: attribute(), attributeNS(), setAttributeNS()

setAttributeNS ( const QString &, const QString &, const QString & )

voidsetAttributeNS(const QString &namespaceUri,
const QString &name,
const QString &value
)

Adds an attribute with the given name in namespaceUri with value. If an attribute with the same name exists, its value is replaced by value.

See also: attributeNS(), attribute(), setAttribute()

setFocus ( )

voidsetFocus()

Gives keyboard input focus to this element

See also: hasFocus()

setInnerXml ( const QString & )

voidsetInnerXml(const QString &markup)

Replaces the contents of this element with markup. The string may contain HTML or XML tags, which is parsed and formatted before insertion into the document.

Note:

This is currently implemented for (X)HTML elements only.

See also: toInnerXml(), toOuterXml(), setOuterXml()

setOuterXml ( const QString & )

voidsetOuterXml(const QString &markup)

Replaces the contents of this element as well as its own tag with markup. The string may contain HTML or XML tags, which is parsed and formatted before insertion into the document.

Note:

This is currently only implemented for (X)HTML elements.

See also: toOuterXml(), toInnerXml(), setInnerXml()

setPlainText ( const QString & )

voidsetPlainText(const QString &text)

Replaces the existing content of this element with text.

This is equivalent to setting the HTML innerText property.

See also: toPlainText()

setStyleProperty ( const QString &, const QString & )

voidsetStyleProperty(const QString &name,
const QString &value
)

Sets the value of the inline style with the given name to value.

Setting a value, does not necessarily mean that it will become the applied value, due to the fact that the style property's value might have been set earlier with a higher priority in external or embedded style declarations.

In order to ensure that the value will be applied, you may have to append "!important" to the value.

styleProperty ( const QString &, StyleResolveStrategy )

QString styleProperty(const QString &name,
StyleResolveStrategystrategy
)const

Returns the value of the style with the given name using the specified strategy. If a style with name does not exist, an empty string is returned.

In CSS, the cascading part depends on which CSS rule has priority and is thus applied. Generally, the last defined rule has priority. Thus, an inline style rule has priority over an embedded block style rule, which in return has priority over an external style rule.

If the "!important" declaration is set on one of those, the declaration receives highest priority, unless other declarations also use the "!important" declaration. Then, the last "!important" declaration takes predecence.

See also: setStyleProperty()

tagName ( )

QString tagName()const

Returns the tag name of this element.

See also: geometry()

takeFromDocument ( )

QWebElement &takeFromDocument()

Removes this element from the document and returns a reference to it.

The element is still valid after removal, and can be inserted into other parts of the document.

See also: removeAllChildren(), removeFromDocument()

toInnerXml ( )

QString toInnerXml()const

Returns the XML content between the element's start and end tags.

Note:

This is currently implemented for (X)HTML elements only.

See also: setInnerXml(), setOuterXml(), toOuterXml()

toOuterXml ( )

QString toOuterXml()const

Returns this element converted to XML, including the start and the end tags as well as its attributes.

Note:

This is currently implemented for (X)HTML elements only.

See also: setOuterXml(), setInnerXml(), toInnerXml()

toPlainText ( )

QString toPlainText()const

Returns the text between the start and the end tag of this element.

This is equivalent to reading the HTML innerText property.

See also: setPlainText()

toggleClass ( const QString & )

voidtoggleClass(const QString &name)

Adds the specified class with the given name if it is not present. If the class is already present, it will be removed.

webFrame ( )

QWebFrame *webFrame()const

Returns the web frame which this element is a part of. If the element is a null element, null is returned.