在我的应用程序中使用QWebEngineView的用户填写了一些表格。此表单使用发布方法将数据提交到服务器。如何从用户的身体请求中获取参数?
我找到了QWebEngineUrlRequestInterceptor之类的东西,但它仅适用于URL。
答案 0 :(得分:1)
您可以使用QWebEnginePage::acceptNavigationRequest。
无论何时提交表单,您都可以使用JavaScript获取输入内容,然后接受请求以照常进行。
答案 1 :(得分:0)
像Anmol Gautam所说的那样,您需要重新实现QWebEnginePage::acceptNavigationRequest函数并使用JavaScript获取所需的数据。
下面是一个示例:
mywebpage.h
#include <QWebEnginePage>
class MyWebPage : public QWebEnginePage
{
Q_OBJECT
public:
explicit MyWebPage(QWebEngineProfile *profile = Q_NULLPTR, QObject *parent = Q_NULLPTR);
protected:
bool acceptNavigationRequest(const QUrl & url, QWebEnginePage::NavigationType type, bool isMainFrame);
}
mywebpage.cpp
MyWebPage::MyWebPage(QWebEngineProfile *profile, QObject *parent):QWebEnginePage(profile, parent),
{
//...
}
bool MyWebPage::acceptNavigationRequest(const QUrl & url, QWebEnginePage::NavigationType type, bool isMainFrame)
{
if(type == QWebEnginePage::NavigationTypeFormSubmitted)
{
qDebug() << "[FORMS] Submitted" << url.toString();
QString jsform = "function getformsvals()"
"{var result;"
"for(var i = 0; i < document.forms.length; i++){"
"for(var x = 0; x < document.forms[i].length; x++){"
"result += document.forms[i].elements[x].name + \" = \" +document.forms[i].elements[x].value;"
"}}"
"return result;} getformsvals();";
this->runJavaScript(jsform, [](const QVariant &result){ qDebug() << "[FORMS] found: " << result; });
}
return true;
}
在调用WebViews加载函数之前,使用QWebEngineView::setPage将WebPage子类设置为WebView。
的更多信息的链接