这是我的.pro文件的完整内容(我已指定添加网络模块):
QT += quick virtualkeyboard network
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++14
SOURCES += \
main.cpp
RESOURCES += qml.qrc \
resources/icon.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
但是,当我在 QML 文件中使用 QNetwork 模块时,如下所示:
import QtNetwork 2.15
我被指出了这个问题:
QML module not found (QNetwork)
Import paths:E:/Qt/5.15.2/msvc2019/qml
For qmake projects, use the QML_IMPORT_PATH variable to add import paths.
For Qbs projects, declare and set a qmllmportPaths property in your product to add import paths.
For qmlproject projects, use the importPaths property to add import paths.
For CMake projects, make sure QML_IMPORT_PATH variable is in CMakeCache.txt.
For qmlRegister.. calls, make sure that you define the Module URl as a string literal.
我检查了文件夹'E:\Qt .15.2\msvc2019',确实没有找到任何与QNetwork模块相关的文件。
发生什么事了?我该如何使用这个模块?
我尝试过网上找到的各种解决方案,但其中很多都是针对Visual Studio IDE的。实际上,我使用的是 Windows 10 22H2 和 QT 5.15、MSVC2019 32 位编译器,我的集成开发环境 (IDE) 是 QT Creator。
遇到问题后,我已将QT更新到最新版本,并检查了相关组件的安装状态。下面是QT的安装状态:
首先,让我们一次解决一个问题。 对于“模块'QtNetwork'未安装”问题,我发现它确实不存在于QML中(至少对于我的QT环境不存在)。如果你想使用网络模块,你应该使用
XMLHttpRequest QML Type
。根据官方文档,你应该添加以下代码:
import QtQml
之后,您可以像这样使用网络模块(以下示例演示如何发送请求并读取响应):
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import "request.js" as XHR
ApplicationWindow {
width: 640
height: 640
visible: true
ColumnLayout {
anchors.fill: parent
RowLayout {
Layout.fillWidth: true
TextField {
id: urlTextField
text: "https://www.example.com/index.html"
Layout.fillWidth: true
}
Button {
text: qsTr("Send!")
onClicked: XHR.sendRequest(urlTextField.text, function(response) {
statusTextField.text = response.status;
let isPlainText = response.contentType.length === 0
contentTypeTextField.text = isPlainText ? "text" : response.contentType;
if (isPlainText)
contentTextArea.text = response.content;
});
}
}
GridLayout {
columns: 2
Layout.fillWidth: true
Label {
text: qsTr("Status code")
Layout.fillWidth: true
}
Label {
text: qsTr("Response type")
Layout.fillWidth: true
}
TextField {
id: statusTextField
Layout.fillWidth: true
}
TextField {
id: contentTypeTextField
Layout.fillWidth: true
}
}
Flickable {
clip: true
contentWidth: contentTextArea.width
contentHeight: contentTextArea.height
Text {
id: contentTextArea
}
Layout.fillWidth: true
Layout.fillHeight: true
ScrollBar.vertical: ScrollBar {}
ScrollBar.horizontal: ScrollBar {}
}
}
}
前面的代码片段将按钮的单击信号连接到外部 sendRequest 函数。资源 URL 作为第一个参数传递,处理 UI 更新的回调函数作为第二个参数传递。 sendRequest 函数存在于外部 request.js 文件中,可以这样实现:
function sendRequest(url, callback)
{
let request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === XMLHttpRequest.DONE) {
let response = {
status : request.status,
headers : request.getAllResponseHeaders(),
contentType : request.responseType,
content : request.response
};
callback(response);
}
}
request.open("GET", url);
request.send();
}