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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xml:lang="de" lang="de"><head>
    <title>Form test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    </head>
    <body>
    <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">
    <br>
    </form></body></html>
    @

    That in browser looks following
    !http://i1177.photobucket.com/albums/x353/michal_g81/picture.png(Picture)!

    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
    }
    else
    {
        //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.

    Thanks.



  • 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
    }
    else
    {
    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
            break;
        }
    }
    

    }
    @

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.