acceptNavigationRequest()
It is a function in the Qt WebEngine module that is used to decide whether to accept navigation requests when the web page is loaded. It can be used to customize the behavior of handling link clicks or page navigation.
This function is usually overridden in classes that inherit from QWebEngineView
or QWebEnginePage
. Its function prototype is as follows:
bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame);
Parameter Description:
url
: The URL of the navigation request.type
: Type of navigation request, such as click link, back, forward, etc.isMainFrame
: Indicates whether the request comes from the Main Frame.
The return value is a Boolean type, indicating whether to accept the navigation request. If true
is returned, navigation continues; if false
is returned, navigation is canceled.
Example usage:
class MyWebView : public QWebEngineView
{
protected:
bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame) override
{
if (type == QWebEnginePage::NavigationTypeLinkClicked) {
// 处理链接点击行为,例如在新窗口打开链接
QDesktopServices::openUrl(url);
return false; // 取消导航
}
return true; // 其他类型的导航请求继续导航
}
};
In the above example, we created a custom class that inherits from QWebEngineView
and overrides Function. In the function, we check whether the type of navigation request is a link click (), if so, use to open the link in a new window and return a> to continue navigation. Cancel navigation; otherwise, for other types of navigation requests, returnMyWebView
acceptNavigationRequest()
NavigationTypeLinkClicked
QDesktopServices::openUrl()
false
true
By overriding theacceptNavigationRequest()
function, you can implement customized navigation behaviors, such as intercepting navigation under specific conditions, modifying navigation targets, etc.
To summarize, the acceptNavigationRequest()
function is the function in the Qt WebEngine module that is used to decide whether to accept the navigation request. By overriding this function, you can implement custom navigation behavior and logic.