QWebEngineView可以和js交互需要使用QWebChannel,如果不使用的话,js可以请求自定义网址协议,相当于请求服务器,但是不用Qt专门做服务器,不占用系统端口。
如果结合系统自定义URL注册,可以达到访问自定义URL,启动我们的Qt程序,程序里再处理自定义URL网址
1.继承QWebEngineUrlSchemeHandler 创建协议处理类
class AMUrlSchemeHandler : public QWebEngineUrlSchemeHandler
{
Q_OBJECT
public:
AMUrlSchemeHandler(QObject *parent);
~AMUrlSchemeHandler();
virtual void requestStarted(QWebEngineUrlRequestJob* pReqJob);
};
2. 在requestStarted编写响应内容
void AMUrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob* pReqJob)
{
QJsonObject jsonObj;
jsonObj.insert("requestUrl", pReqJob->requestUrl().toString());
jsonObj.insert("requestMethod", QString(pReqJob->requestMethod()));
jsonObj.insert("initiator", pReqJob->initiator().toString());
if (pReqJob->requestUrl().toString() == "amurl://www.baidu.com")
{
if (pReqJob->requestMethod() == "GET")
{
jsonObj.insert("result", "GET 0");
}
else if (pReqJob->requestMethod() == "POST")
{
jsonObj.insert("result", "POST 1");
}
else
{
pReqJob->fail(QWebEngineUrlRequestJob::RequestDenied);
return;
}
}
QBuffer* buffer = new QBuffer;
connect(pReqJob, SIGNAL(destroyed()), buffer, SLOT(deleteLater()));
buffer->open(QIODevice::WriteOnly);
QString strJson = QJsonDocument(jsonObj).toJson(QJsonDocument::Indented);
buffer->write(strJson.toUtf8());
buffer->close();
pReqJob->reply("application/json", buffer);
}
3.在QApplication初始化前注册自定义协议QWebEngineUrlScheme::registerScheme(scheme);
4. 再安装协议处理类installUrlSchemeHandler(“amurl”, handler);
int main(int argc, char *argv[])
{
QWebEngineUrlScheme scheme("amurl");
scheme.setSyntax(QWebEngineUrlScheme::Syntax::Path);
scheme.setFlags(QWebEngineUrlScheme::SecureScheme);
QWebEngineUrlScheme::registerScheme(scheme);
QApplication a(argc, argv);
AMUrlSchemeHandler* handler = new AMUrlSchemeHandler(&a);
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler("amurl", handler);
StudyQWebEngineUrlScheme w;
w.show();
return a.exec();
}
5. 我们可以访问试试
StudyQWebEngineUrlScheme::StudyQWebEngineUrlScheme(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QVBoxLayout* pLayoutMain = new QVBoxLayout(ui.centralWidget);
ui.centralWidget->setLayout(pLayoutMain);
pLayoutMain->setContentsMargins(0, 30, 0, 0);
{
m_pView = new QWebEngineView(ui.centralWidget);
pLayoutMain->addWidget(m_pView);
{
QWebEnginePage* pWebEnginePage = new QWebEnginePage(m_pView);
m_pView->setPage(pWebEnginePage);
}
QWebEngineView* view1 = new QWebEngineView(m_pView);
pLayoutMain->addWidget(view1);
m_pView->page()->setDevToolsPage(view1->page());
}
//Get请求
m_pView->load(QUrl("amurl://www.Baidu.com"));
connect(ui.btnPost, &QPushButton::clicked, this, &StudyQWebEngineUrlScheme::sltBtnPostClicked);
connect(ui.btnOther, &QPushButton::clicked, this, &StudyQWebEngineUrlScheme::sltBtnOtherClicked);
}
void StudyQWebEngineUrlScheme::sltBtnPostClicked()
{
QWebEngineHttpRequest httpReq;
httpReq.setUrl(QUrl("Amurl://www.Baidu.com"));
httpReq.setMethod(QWebEngineHttpRequest::Post);
m_pView->load(httpReq);
//POST请求好像无效,无法处理
}
void StudyQWebEngineUrlScheme::sltBtnOtherClicked()
{
QWebEngineHttpRequest httpReq;
httpReq.setUrl(QUrl("Amurl://www.Baidu.coM"));
httpReq.setMethod(QWebEngineHttpRequest::Method(2));
m_pView->load(httpReq);
//Method>=2,请求都转为Get请求了
}
官方示例:WebEngine Widgets WebUI 范例| Qt WebEngine 5.15.1 -中文文档编制,帮助,手册,教程
本文示例代码:
所有代码的7z压缩后的Base64编码如下:
(注意复制出来是一行,再Base64 Decode,保存成7z文件,再解压即可)
N3q8ryccAAR4F49KLwwAAAAAAAAjAAAAAAAAAIm4NCHgLc0L服务器托管网HV0AEZpJxkcPE6IAdxz7hl38RS8dsuo5N/0pYz/q4AFep9Lb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OQ2x5mtgjTXcBslxWAzAXmqWsgEC7QwIFhZRXyRa5U1OtDdYwx1jbvySnq6AN7DcJFpB4RUeIXZcg3Ccm2lyzqYAISxl0FardZ7+m0VZHvxXuLaGIRmz/FF65FnXhDKmvHyqPCcW5vgq/CVSKtHSdfHiStbt7hiJy5WT7+pilDSHlH5wQAkF2n9EbZn8/D7N1Fp7731mSo+F8hkxfoCvK+DXTty8mBdeGaV7vn5BxkNbMsIqXIAwZONAk5S3aeR1zd6vD9rVNbbyMDQsOvROL8CpA9R2aFRVIiKfdUOArShOhRL9/aO5YaSpYFpRLzM1c6qhjqQN0dl1IZ4FuZ7pftptge2BsAx0q9HAZ2h+tZ/FFd33hocOT32hxFoUrwzaAbbiY4rPvRhAEgngu/syCJ70fTeS8GkuZ6eE5CwjE9K42fX2qj8W+KBlVNCm7nwa/iv0l81qTswHzMpRgKBFZCCGztasl33G8mjsqvnSRdD8cCy0LfYyEydYovxv5mXlwVnQAAAIEzB64P1aaotlck0/6zcCII0wW7LyNjZz5ND7Hh/dFsTY8C/kY0H9xvx1hylYdMIPb+MzOdNRw+Oo88sp1lRKLwC75h8/h49xScfa2E2wLG0QQzdUQJGyVF6dfffZYnKyg7wGkPH5yw81P9gc/M+R1+YxW1GUT2PPFkQh3IiukiTRALZ0r3R/qA1RO1dSXpanzV5yyGPp/Qmpwe5sgNFkoaXUTn3rzIrhADD97hTBQgRmF7vFnGFl1FQwve2WzqzZU1UMyF7Wmm/39orRNvmBF5O4ZWGKr6CoRZPZqCwUtlTcfXnn/smlZ3/gBizvtwozFw+uRLOW49wZSZ0YNqUWaUXlLysObNgBcGiyUBCYEKAAcLAQABIwMBAQVdABAAAAyCXgoB1LGF6gAA
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
router的使用(5+2) 5个基础步骤: 1.在终端执行yarn add vue-router@3.6.5,安装router插件 yarn add vue-router@3.6.5 2.在文件的main.js中引入router插件 import VueRo…