我正在使用 QT WebEngine 来制作一个适合我工作时要求的桌面网络浏览器。不幸的是,我需要将一些带有 HTTP 标头的数据发送到站点。我遇到了QWebEngineUrlRequestInfo类的void QWebEngineUrlRequestInfo::setHttpHeader(const QByteArray &name, const QByteArray &value)
方法,但我真的不知道如何在代码中使用它。到目前为止,这是我的代码:
import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import QtWebEngine 1.3
ApplicationWindow {
visible: true
width: 320
height: 240
title: qsTr("Browser")
flags: Qt.WindowStaysOnTopHint
WebEngineView {
anchors.fill: parent
url: "some http url here"
Rectangle{
height: 100
width: height
}
}
}
这是我的 main.cpp 文件:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtWebEngine/qtwebengineglobal.h>
#include <QWebEngineUrlRequestInfo>
#include <QByteArray>
int main(int argc, char *argv[])
{
QByteArray key, value;
key.append("SomeKey");
value.append("SomeValue");
QWebEngineUrlRequestInfo url;//Won't work because its constructor is private
url.setHttpHeader(key, value);
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QtWebEngine::initialize();
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
return app.exec();
}
由于上面的代码抛出了一个错误,我实在想不出解决这个问题的办法。TIA