Important: Please read the Qt Code of Conduct -

QtWebKit - how to get information about <label> element on which user clicked

  • Hi,

    I have such web page

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xml:lang="de" lang="de"><head>
    <title>Form test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <form action="../PChecker" method="get">
    <br><br><label for="male">Male</label>
    <input class="noborder" name="radiobuttonMH" value="male" type="radio" id="male">
    <br><label for="female">Female</label>
    <input class="noborder" name="radiobuttonMH" value="female" type="radio" id="female">

    That in browser looks following

    And now my problem:
    User click on for instance Female to select radio button. This is label in HTML code. I would like to be able to get this element as QWebElement.
    So I did following
    void CWebView::mousePressEvent(QGraphicsSceneMouseEvent * a_pMouseEvent)
    QWebFrame * pFrame = m_oPage.frameAt(a_pMouseEvent->pos().toPoint());
    QWebHitTestResult oHitResult = pFrame->hitTestContent(a_pMouseEvent->pos().toPoint());
    QWebElement oWebElement = a_oHitResult.element();

    if (oWebElement.isNull())
        //and the element is always NULL
        //Do some action


    Unfortunetly, I always have information that element is NULL. When I directly clicked on circle that it is OK.

    For simplicity I did not put here checking of pointers i.e. to frame and so on.

    Do you have an idea what can be wrong? How to get <label> as a QWebElement when the user click on it.


  • Did you check if the position of the mouse cursor returned by a_pMouseEvent->pos().toPoint() is correct?

  • Yes, position is correct.
    I can get HTML code from frame. I have also information about size (QRect) of element under cursor. Even if this element is NULL.

  • Hmmm. Maybe the QWebElement of your label is interpreted as a null element. I had some trouble once appending stuff inside a QWebElement of a <header> (QWebElement::appendInside(..)), nothing was appended because the element was interpreted as a null element.

  • Yes, I put this information that my <label> is interpreted as a null element. And I cannot understand why?

  • I look at problem also from other perspective. QWebElement from QWebHitTestResult can give information about size of element on which user make a click. I used this size to check if such element is on page.

    QWebElementCollection labelsCollection = a_pWebFrame->findAllElements("label");
    if (0 == labelsCollection.count())
    //lack of labels
    foreach (QWebElement element, labelsCollection)
    QRect modification = element.geometry();
    modification.adjust(0,1,0,0); //add 1 to y axis, because geometry returns smaller size than real geometry

        //a_elementRect is rectangle from hit test
        if (a_elementRect == modification)
           retValElement = element; //we have label that was previously null


    I am able to find <label> in such way and after assignment to QWebElement I can do all operation on it, as it is not null element.

    I used it as a workaround, but I do not think that it is a perfect solution.

Log in to reply