在我的应用程序中使用QWebEngineView的用户填写一些表单。此表单使用post方法向服务器提交数据。如何从用户的身体请求中获得params?
我已经找到了像QWebEngineUrlRequestInterceptor这样的东西,但它只适用于urls。
发布于 2019-03-05 16:52:28
您可以使用QWebEnginePage:acceptNavigationRequest。
无论何时提交表单,您都可以使用JavaScript获取输入的内容,然后接受照常进行的请求。
发布于 2019-03-06 10:55:11
正如安莫高塔姆所说,您需要重新实现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;
}使用QWebEngineView::setPage在调用WebPage加载函数之前将WebPage子类设置为WebView。
以下是有关HTML DOM窗体集合的更多信息的链接
https://stackoverflow.com/questions/55007679
复制相似问题