0

Looking at the signals in the QtWebKit API, I failed to find anything that would seem to me to be what I am looking for.

  • QWebView
    • linkClicked() seems to be the closest, but a reset button is no link, and definitely does not point to an URL.
  • QWebPage
    • I considered the following signals (judging by their name), but according to their description none of them match my purpose either: contentsChanged(), contentsChanged(), contentsChanged(), selectionChanged().
  • QWebFrame
    • None of its signals matches my purpose.
  • QWebElement
    • Here I can see how to get an object representing the button(s), but it has no signals whatsoever.

I want to catch a click in a reset button in order to store the data in the form before it gets cleared, so it can be restored later.

For now, I did manage to retrieve the buttons as a QWebElementCollection of QWebElement objects, and I can modify them, but I do not know how to get them to send a signal upon click, or something similar.

// Get reset buttons.
QWebElementCollection inputResets = mainFrame()->documentElement().findAll("input[type=reset]");
inputResets += mainFrame()->documentElement().findAll("button[type=reset]");

// Change their text (just a test).
foreach(QWebElement element, inputResets)
{
    element.setPlainText("Worked!");
}
Gallaecio
  • 3,620
  • 2
  • 25
  • 64

2 Answers2

0

Well, I got it working with this, although I do not think it is the best approach:

bool EventFilter::eventFilter(QObject* object, QEvent* event)
{
    if (event->type() == QEvent::MouseButtonRelease)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if (mouseEvent->button() == Qt::LeftButton)
        {
            QWebView *view = dynamic_cast<QWebView*>(object);

            QPoint pos = view->mapFromGlobal(mouseEvent->globalPos());

            QWebFrame *frame = view->page()->frameAt(mouseEvent->pos());
            if (frame != NULL)
            {
                // Get the existing reset buttons.
                QWebElementCollection inputResets = frame->documentElement().findAll("input[type=reset]");
                inputResets += frame->documentElement().findAll("button[type=reset]");

                // Check if any of them is at the clicked position.
                foreach(QWebElement element, inputResets)
                {
                    if (element.geometry().contains(pos))
                    {
                        qDebug() << "Clicked element tag:" << element.localName();

                        return QObject::eventFilter(object, event);
                    }
                }
            }
        }
    }

    return QObject::eventFilter(object, event);
}
Gallaecio
  • 3,620
  • 2
  • 25
  • 64
0

You can probably accomplish this with Qt WebKit Bridge.

bendiy
  • 595
  • 1
  • 7
  • 15