【发布时间】:2017-02-21 15:40:12
【问题描述】:
我正在尝试将 QnetworkRequest 传递给位于我的 main.qml 文件中的 webView,而不是“url”。我通过引用 webView 对象和 setproperty 函数来传递 url。但是,还没有找到正确的函数,并且真的不知道从哪里开始创建新函数或修改现有的 webView 代码以使其正常工作。有没有办法将源代码编辑到 .qml 文件中的 webView。当然我只是在学习 QT 框架。
我尝试过 WebengineView,但 Webview 在加载页面时要快得多。这对应用程序至关重要
我想真正的问题是 Webview 是如何做一个请求的,我该如何拦截它?
main.qml
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtWebView 1.1
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.2
ApplicationWindow {
flags: Qt.FramelessWindowHint
visible: true
x: 600
y: 400
width: 500
height: 500
title: webView.title
WebView {
id: webView
anchors.fill: parent
objectName: "webView"
//setting this value through main.cpp
// url: "https://www.google.com"
onLoadingChanged: {
if (loadRequest.errorString)
console.error(loadRequest.errorString);
}
}
}
main.cpp
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtWebView::initialize();
QQmlApplicationEngine engine;
//How to Pass THIS request to the webview instead of url?
QNetworkRequest request;
request.setUrl(QUrl("http://google.com"));
request.setRawHeader("Accept-Charset", "UTF-8,*;q=0.5");
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QObject *rootObject = engine.rootObjects().first();
QObject *qmlObject = rootObject->findChild<QObject*>("webView");
//Able to set the URL for the webView:
qmlObject->setProperty("url", "https://www.google.com" );
return app.exec();
}
【问题讨论】:
-
当
WebView的url属性发生更改时,如何处理来自QML 的更改URL?onUrlChanged: { ... }