Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QWebElement Class

TheQWebElement class provides convenient access to DOM elements in aQWebFrame.More...

Header:#include <QWebElement>
Since: Qt 4.6

Public Types

enumStyleResolveStrategy { InlineStyle, CascadedStyle, ComputedStyle }

Public Functions

QWebElement()
QWebElement(const QWebElement & other)
~QWebElement()
voidaddClass(const QString & name)
voidappendInside(const QString & markup)
voidappendInside(const QWebElement & element)
voidappendOutside(const QString & markup)
voidappendOutside(const QWebElement & element)
QStringattribute(const QString & name, const QString & defaultValue = QString()) const
QStringattributeNS(const QString & namespaceUri, const QString & name, const QString & defaultValue = QString()) const
QStringListattributeNames(const QString & namespaceUri = QString()) const
QStringListclasses() const
QWebElementclone() const
QWebElementdocument() const
voidencloseContentsWith(const QWebElement & element)
voidencloseContentsWith(const QString & markup)
voidencloseWith(const QString & markup)
voidencloseWith(const QWebElement & element)
QVariantevaluateJavaScript(const QString & scriptSource)
QWebElementCollectionfindAll(const QString & selectorQuery) const
QWebElementfindFirst(const QString & selectorQuery) const
QWebElementfirstChild() const
QRectgeometry() const
boolhasAttribute(const QString & name) const
boolhasAttributeNS(const QString & namespaceUri, const QString & name) const
boolhasAttributes() const
boolhasClass(const QString & name) const
boolhasFocus() const
boolisNull() const
QWebElementlastChild() const
QStringlocalName() const
QStringnamespaceUri() const
QWebElementnextSibling() const
QWebElementparent() const
QStringprefix() const
voidprependInside(const QString & markup)
voidprependInside(const QWebElement & element)
voidprependOutside(const QString & markup)
voidprependOutside(const QWebElement & element)
QWebElementpreviousSibling() const
voidremoveAllChildren()
voidremoveAttribute(const QString & name)
voidremoveAttributeNS(const QString & namespaceUri, const QString & name)
voidremoveClass(const QString & name)
voidremoveFromDocument()
voidrender(QPainter * painter)
voidrender(QPainter * painter, const QRect & clip)
voidreplace(const QString & markup)
voidreplace(const QWebElement & element)
voidsetAttribute(const QString & name, const QString & value)
voidsetAttributeNS(const QString & namespaceUri, const QString & name, const QString & value)
voidsetFocus()
voidsetInnerXml(const QString & markup)
voidsetOuterXml(const QString & markup)
voidsetPlainText(const QString & text)
voidsetStyleProperty(const QString & name, const QString & value)
QStringstyleProperty(const QString & name, StyleResolveStrategy strategy) const
QStringtagName() const
QWebElement &takeFromDocument()
QStringtoInnerXml() const
QStringtoOuterXml() const
QStringtoPlainText() const
voidtoggleClass(const QString & name)
QWebFrame *webFrame() const
booloperator!=(const QWebElement & o) const
QWebElement &operator=(const QWebElement & other)
booloperator==(const QWebElement & o) const

Detailed Description

TheQWebElement class provides convenient access to DOM elements in aQWebFrame.

AQWebElement 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 usingQWebFrame::documentElement().

Specific elements can be accessed usingfindAll() andfindFirst(). These elements are identified using CSS selectors. The code snippet below demonstrates the use offindAll().

QWebElement document= frame->documentElement();/* Assume the document has the following structure:       <p class=intro>         <span>Intro</span>         <span>Snippets</span>       </p>       <p>         <span>Content</span>         <span>Here</span>       </p>    */QWebElementCollection allSpans= document.findAll("span");QWebElementCollection introSpans= document.findAll("p.intro span");

The first list contains allspan elements in the document. The second list containsspan elements that are children ofp, classified withintro.

UsingfindFirst() is more efficient than callingfindAll(), and extracting the first element only in the list returned.

Alternatively you can traverse the document manually usingfirstChild() andnextSibling():

    frame->setHtml("<html><body><p>First Paragraph</p><p>Second Paragraph</p></body></html>");QWebElement doc= frame->documentElement();QWebElement body= doc.firstChild();QWebElement firstParagraph= body.firstChild();QWebElement secondParagraph= firstParagraph.nextSibling();

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

QWebElement firstTextInput= document.findFirst("input[type=text]");QString storedText= firstTextInput.attribute("value");

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:

QWebElement firstTextInput= document.findFirst("input[type=text]");    textInput.setAttribute("value", storedText);

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:

QWebElement document= frame->documentElement();/* Assume that the document has the following structure:        <form name="myform" action="submit_form.asp" method="get">            <input type="text" name="myfield">            <input type="submit" value="Submit">        </form>     */QWebElement button= document.findFirst("input[type=submit]");    button.evaluateJavaScript("this.click()");

The underlying content ofQWebElement is explicitly shared. Creating a copy of aQWebElement 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 withtoPlainText(); to XHTML usingtoInnerXml(). To include the element's tag in the output, usetoOuterXml().

It is possible to replace the contents of child elements usingsetPlainText() andsetInnerXml(). To replace the element itself and its contents, usesetOuterXml().

Examples

TheDOM Traversal Example shows one way to traverse documents in a running example.

TheSimple Selector Example can be used to experiment with the searching features of this class and provides sample code you can start working with.

Member Type Documentation

enum QWebElement::StyleResolveStrategy

This enum describes howQWebElement'sstyleProperty resolves the given property name.

ConstantValueDescription
QWebElement::InlineStyle0Return the property value as it is defined in the element, without respecting style inheritance and other CSS rules.
QWebElement::CascadedStyle1The property's value is determined using the inheritance and importance rules defined in the document's stylesheet.
QWebElement::ComputedStyle2The property's value is the absolute value of the style property resolved from the environment.

Member Function Documentation

QWebElement::QWebElement()

Constructs a null web element.

QWebElement::QWebElement(constQWebElement & other)

Constructs a copy ofother.

QWebElement::~QWebElement()

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

void QWebElement::addClass(constQString & name)

Adds the specified class with the givenname to the element.

void QWebElement::appendInside(constQString & markup)

Appends the result of parsingmarkup as the element's last child.

Calling this function on a null element does nothing.

See alsoprependInside(),prependOutside(), andappendOutside().

void QWebElement::appendInside(constQWebElement & element)

Appends the givenelement as the element's last child.

Ifelement is the child of another element, it is re-parented to this element. Ifelement 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 alsoprependInside(),prependOutside(), andappendOutside().

void QWebElement::appendOutside(constQString & markup)

Inserts the result of parsingmarkup after this element.

Calling this function on a null element does nothing.

See alsoappendInside(),prependInside(), andprependOutside().

void QWebElement::appendOutside(constQWebElement & element)

Inserts the givenelement after this element.

Ifelement 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 alsoappendInside(),prependInside(), andprependOutside().

QString QWebElement::attribute(constQString & name, constQString & defaultValue = QString()) const

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

See alsosetAttribute(),setAttributeNS(), andattributeNS().

QString QWebElement::attributeNS(constQString & namespaceUri, constQString & name, constQString & defaultValue = QString()) const

Returns the attribute with the givenname innamespaceUri. If the attribute does not exist,defaultValue is returned.

See alsosetAttributeNS(),setAttribute(), andattribute().

QStringList QWebElement::attributeNames(constQString & namespaceUri = QString()) const

Return the list of attributes for the namespace given asnamespaceUri.

See alsoattribute() andsetAttribute().

QStringList QWebElement::classes() const

Returns the list of classes of this element.

QWebElement QWebElement::clone() const

Returns a clone of this element.

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

See alsoappendInside(),prependInside(),prependOutside(), andappendOutside().

QWebElement QWebElement::document() const

Returns the document which this element belongs to.

void QWebElement::encloseContentsWith(constQWebElement & element)

Encloses the contents of this element withelement. This element becomes the child of the deepest descendant withinelement.

### illustration

See alsoencloseWith().

void QWebElement::encloseContentsWith(constQString & markup)

Encloses the contents of this element with the result of parsingmarkup. This element becomes the child of the deepest descendant withinmarkup.

See alsoencloseWith().

void QWebElement::encloseWith(constQString & markup)

Encloses this element with the result of parsingmarkup. This element becomes the child of the deepest descendant withinmarkup.

See alsoreplace().

void QWebElement::encloseWith(constQWebElement & element)

Encloses this element withelement. This element becomes the child of the deepest descendant withinelement.

See alsoreplace().

QVariant QWebElement::evaluateJavaScript(constQString & scriptSource)

ExecutesscriptSource with this element asthis object.

QWebElementCollection QWebElement::findAll(constQString & selectorQuery) const

Returns a new list of child elements matching the given CSS selectorselectorQuery. 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 alsofindFirst().

QWebElement QWebElement::findFirst(constQString & selectorQuery) const

Returns the first child element that matches the given CSS selectorselectorQuery.

Standard CSS2 selector syntax is used for the query.

Note:This search is performed recursively.

See alsofindAll().

QWebElement QWebElement::firstChild() const

Returns the element's first child.

See alsolastChild(),previousSibling(), andnextSibling().

QRect QWebElement::geometry() const

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

See alsotagName().

bool QWebElement::hasAttribute(constQString & name) const

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

See alsoattribute() andsetAttribute().

bool QWebElement::hasAttributeNS(constQString & namespaceUri, constQString & name) const

Returns true if this element has an attribute with the givenname, innamespaceUri; otherwise returns false.

See alsoattributeNS() andsetAttributeNS().

bool QWebElement::hasAttributes() const

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

See alsoattribute() andsetAttribute().

bool QWebElement::hasClass(constQString & name) const

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

bool QWebElement::hasFocus() const

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

See alsosetFocus().

bool QWebElement::isNull() const

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

QWebElement QWebElement::lastChild() const

Returns the element's last child.

See alsofirstChild(),previousSibling(), andnextSibling().

QString QWebElement::localName() const

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

QString QWebElement::namespaceUri() const

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

QWebElement QWebElement::nextSibling() const

Returns the element's next sibling.

See alsofirstChild(),previousSibling(), andlastChild().

QWebElement QWebElement::parent() const

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

QString QWebElement::prefix() const

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

void QWebElement::prependInside(constQString & markup)

Prepends the result of parsingmarkup as the element's first child.

Calling this function on a null element does nothing.

See alsoappendInside(),prependOutside(), andappendOutside().

void QWebElement::prependInside(constQWebElement & element)

Prependselement as the element's first child.

Ifelement is the child of another element, it is re-parented to this element. Ifelement 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 alsoappendInside(),prependOutside(), andappendOutside().

void QWebElement::prependOutside(constQString & markup)

Inserts the result of parsingmarkup before this element.

Calling this function on a null element does nothing.

See alsoappendInside(),prependInside(), andappendOutside().

void QWebElement::prependOutside(constQWebElement & element)

Inserts the givenelement before this element.

Ifelement 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 alsoappendInside(),prependInside(), andappendOutside().

QWebElement QWebElement::previousSibling() const

Returns the element's previous sibling.

See alsofirstChild(),nextSibling(), andlastChild().

void QWebElement::removeAllChildren()

Removes all children from this element.

See alsoremoveFromDocument() andtakeFromDocument().

void QWebElement::removeAttribute(constQString & name)

Removes the attribute with the givenname from this element.

See alsoattribute(),setAttribute(), andhasAttribute().

void QWebElement::removeAttributeNS(constQString & namespaceUri, constQString & name)

Removes the attribute with the givenname, innamespaceUri, from this element.

See alsoattributeNS(),setAttributeNS(), andhasAttributeNS().

void QWebElement::removeClass(constQString & name)

Removes the specified class with the givenname from the element.

void QWebElement::removeFromDocument()

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

See alsoremoveAllChildren() andtakeFromDocument().

void QWebElement::render(QPainter * painter)

Render the element intopainter .

void QWebElement::render(QPainter * painter, constQRect & clip)

Render the element intopainter clipping toclip.

void QWebElement::replace(constQString & markup)

Replaces this element with the result of parsingmarkup.

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

See alsoencloseWith().

void QWebElement::replace(constQWebElement & element)

Replaces this element withelement.

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

See alsoencloseWith().

void QWebElement::setAttribute(constQString & name, constQString & value)

Adds an attribute with the givenname andvalue. If an attribute with the same name exists, its value is replaced byvalue.

See alsoattribute(),attributeNS(), andsetAttributeNS().

void QWebElement::setAttributeNS(constQString & namespaceUri, constQString & name, constQString & value)

Adds an attribute with the givenname innamespaceUri withvalue. If an attribute with the same name exists, its value is replaced byvalue.

See alsoattributeNS(),attribute(), andsetAttribute().

void QWebElement::setFocus()

Gives keyboard input focus to this element

See alsohasFocus().

void QWebElement::setInnerXml(constQString & markup)

Replaces the contents of this element withmarkup. 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 alsotoInnerXml(),toOuterXml(), andsetOuterXml().

void QWebElement::setOuterXml(constQString & markup)

Replaces the contents of this element as well as its own tag withmarkup. 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 alsotoOuterXml(),toInnerXml(), andsetInnerXml().

void QWebElement::setPlainText(constQString & text)

Replaces the existing content of this element withtext.

This is equivalent to setting the HTML innerText property.

See alsotoPlainText().

void QWebElement::setStyleProperty(constQString & name, constQString & value)

Sets the value of the inline style with the givenname tovalue.

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.

See alsostyleProperty().

QString QWebElement::styleProperty(constQString & name,StyleResolveStrategy strategy) const

Returns the value of the style with the givenname using the specifiedstrategy. If a style withname 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 alsosetStyleProperty().

QString QWebElement::tagName() const

Returns the tag name of this element.

See alsogeometry().

QWebElement & 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 alsoremoveAllChildren() andremoveFromDocument().

QString QWebElement::toInnerXml() const

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

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

Note:The format of the markup returned will obey the namespace of the document containing the element. This means the return value will obey XML formatting rules, such as self-closing tags, only if the document is 'text/xhtml+xml'.

See alsosetInnerXml(),setOuterXml(), andtoOuterXml().

QString QWebElement::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.

Note:The format of the markup returned will obey the namespace of the document containing the element. This means the return value will obey XML formatting rules, such as self-closing tags, only if the document is 'text/xhtml+xml'.

See alsosetOuterXml(),setInnerXml(), andtoInnerXml().

QString QWebElement::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 alsosetPlainText().

void QWebElement::toggleClass(constQString & name)

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

QWebFrame * QWebElement::webFrame() const

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

bool QWebElement::operator!=(constQWebElement & o) const

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

QWebElement & QWebElement::operator=(constQWebElement & other)

Assignsother to this element and returns a reference to this element.

bool QWebElement::operator==(constQWebElement & o) const

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

© 2016 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 theGNU 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.


[8]ページ先頭

©2009-2025 Movatter.jp