QT QWebKit所属对象的事件

1. QWebView信号:

QWebView继承了QWidget的4个事件和QObject的2个事件。

void iconChanged()
void linkClicked(const QUrl & url)
void loadFinished(bool ok)
void loadProgress(int progress)
void loadStarted()
void selectionChanged()
void statusBarMessage(const QString & text)
void titleChanged(const QString & title)
void urlChanged(const QUrl & url)

2. QWebPage信号:

QWebPage继承了QObject的2个事件

void applicationCacheQuotaExceeded(QWebSecurityOrigin * origin, quint64 defaultOriginQuota, quint64 totalSpaceNeeded)
void contentsChanged()
void databaseQuotaExceeded(QWebFrame * frame, QString databaseName)
void downloadRequested(const QNetworkRequest & request)
void featurePermissionRequestCanceled(QWebFrame * frame, QWebPage::Feature feature)
void featurePermissionRequested(QWebFrame * frame, QWebPage::Feature feature)
void frameCreated(QWebFrame * frame)
void geometryChangeRequested(const QRect & geom)
void linkClicked(const QUrl & url)
void linkHovered(const QString & link, const QString & title, const QString & textContent)
void loadFinished(bool ok)
void loadProgress(int progress)
void loadStarted()
void menuBarVisibilityChangeRequested(bool visible)
void microFocusChanged()
void printRequested(QWebFrame * frame)
void repaintRequested(const QRect & dirtyRect)
void restoreFrameStateRequested(QWebFrame * frame)
void saveFrameStateRequested(QWebFrame * frame, QWebHistoryItem * item)
void scrollRequested(int dx, int dy, const QRect & rectToScroll)
void selectionChanged()
void statusBarMessage(const QString & text)
void statusBarVisibilityChangeRequested(bool visible)
void toolBarVisibilityChangeRequested(bool visible)
void unsupportedContent(QNetworkReply * reply)
void viewportChangeRequested()
void windowCloseRequested()

3. QWebFrame信号:

QWebFrame继承了QObject的2个事件

void contentsSizeChanged(const QSize & size)
void iconChanged()
void initialLayoutCompleted()
void javaScriptWindowObjectCleared()
void loadFinished(bool ok)
void loadStarted()
void pageChanged()
void titleChanged(const QString & title)
void urlChanged(const QUrl & url)

4. 注册QT交互对象的时机

为了与HTML交互,我们需要调用

webView->page()->mainFrame()->addToJavaScriptWindowObject("Evnitem", envSce);

向DOM中注入一个交互对象。这个对象的加入时机需要把握。可通过对javaScriptWindowObjectCleared()信号进行处理,调用上面方法加入对象。javaScriptWindowObjectCleared()信号的解释如下:

This signal is emitted whenever the global window object of the JavaScript environment is cleared, e.g., before starting a new load.

If you intend to add QObjects to a QWebFrame using addToJavaScriptWindowObject(), you should add them in a slot connected to this signal. This ensures that your objects remain accessible when loading new URLs.

其方法是

(1)声明一个slot函数

private slots:

    void populateJavaScriptWindowObject();

并实现这个函数

void MainWindow::populateJavaScriptWindowObject()
{
    webView->page()->mainFrame()->addToJavaScriptWindowObject("Evnitem", envSce);
}

(2)将信号与槽连接

    connect(webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),this, SLOT(populateJavaScriptWindowObject()));

 

posted @ 2020-12-31 16:23  小船1968  阅读(160)  评论(0编辑  收藏  举报