1

According to the help of Qt for QWebPage [Slot ShoudInteruptJavaScript], located here:

This function is called when a JavaScript program is running for a long period of time.

If the user wanted to stop the JavaScript the implementation should return true; otherwise false.

The default implementation executes the query using QMessageBox::information with QMessageBox::Yes and QMessageBox::No buttons.

Warning: Because of binary compatibility constraints, this function is not virtual. If you want to provide your own implementation in a QWebPage subclass, reimplement the shouldInterruptJavaScript() slot in your subclass instead. QtWebKit will dynamically detect the slot and call it.


I don't want qt show a message when javascript runnig for long period of time.
So, how can i reimplement ShoudInteruptJavaScript?
and where should i create it?
Please show me a sample Thanks

3 Answers 3

2

All the info you need is in the documentation.

Create a new custom class that inherits from QWebPage, make sure it's a Q_OBJECT to receive signals.

class MyFunkyPage : public QWebPage {
    Q_OBJECT
public slots:
    bool shouldInterruptJavaScript() {
        QApplication::processEvents(QEventLoop::AllEvents, 42);
        // Ignore the error
        return false;
    }
};

Set the page of your QWebView to a custom subclass of QWebPage.

setPage(new MyFunkyPage());

Then when your page gets this signal it won't stop the script from executing, and it won't show a dialog.

Sign up to request clarification or add additional context in comments.

1 Comment

why do you add the processEvents call?
1

The MyFunkyPage solution potentially leaks memory and causes crashes because the object passed to setPage has no parent and setPage does not take ownership. Instead,

class QWebPageWithoutJsWarning : public QWebPage {
    Q_OBJECT
public:
    QWebPageWithoutJsWarning(QObject* parent = 0) : QWebPage(parent) {}
public slots:
    bool shouldInterruptJavaScript() {
        return false;
    }
};

Set the page of your QWebView to the custom subclass of QWebPage, parented on the WebView,

void suppressJSWarning(QWebView& webView) {
    webView.setPage(new QWebPageWithoutJsWarning(&webView));
}

Comments

1

@anson-mackeracher almost had it right.

Qt needs it to be a private slot, not a public one. Here's what works for my class:

class MyFunkyPage : public QWebPage {
  Q_OBJECT

private slots:
  bool shouldInterruptJavaScript() {
    // Ignore the error  (return true to kill the runaway JavaScript)
    return false;
  }
};

Set the page of your QWebView to a custom subclass of QWebPage.

setPage(new MyFunkyPage());

I just tested this with Qt 4.8.4 and it works like a charm. I didn't need the processEvents call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.