module "QtNetwork" is not installed
-
Hello. I am new to QML and am really loving it.
I am trying to make a TCP client app with Qt 6.4.3 but I am getting an error: QML module not found (QtNetwork)
In QT Creator I did: Create Project>Application (Qt)>Qt Quick Application>qmake
This is my qml code:
import QtQuick 2.15 import QtQuick.Window 2.15 import QtNetwork Window { visible: true width: 400 height: 300 title: "TCP Client" property string host: "localhost" property int port: 1234 TextField { id: messageField anchors.top: parent.top anchors.horizontalCenter: parent.horizontalCenter width: parent.width * 0.8 placeholderText: "Enter message" onTextChanged: sendButton.enabled = text.trim().length > 0 } Button { id: sendButton anchors.top: messageField.bottom anchors.horizontalCenter: parent.horizontalCenter text: "Send" enabled: false onClicked: { var socket = new QTcpSocket(); socket.connectToHost(host, port); if (socket.state === QTcpSocket.ConnectedState) { var message = messageField.text.trim(); socket.write(message); socket.flush(); socket.disconnectFromHost(); messageField.text = ""; } else { console.log("Failed to connect to " + host + ":" + port); } } } }
and this is my .pro file code:
QT += quick network SOURCES += \ main.cpp resources.files = main.qml resources.prefix = /$${TARGET} RESOURCES += resources # 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
Where is Qt Network? In the maintenance tool, I can't see Qt Network. Is it a seperate install like qt multimedia?
-
ok figured it out. Here is the working .pro file:
QT += quick quickcontrols2 network CONFIG += c++11 SOURCES += main.cpp RESOURCES += qml.qrc QML_IMPORT_PATH = $$PWD QML_DESIGNER_IMPORT_PATH = $$QML_IMPORT_PATH QQmlApplicationEngine { qmlProtectModule("Qt.Network", 1); } # Additional compiler flags (if any) QMAKE_CXXFLAGS += -Wall # Additional linker flags (if any) QMAKE_LFLAGS += # Additional libraries (if any) LIBS +=
This .pro file specifies that the project requires the QtQuick, QtQuickControls2, and QtNetwork modules, and sets the C++ version to c++11. The main.cpp file is listed as the project source file, and the qml.qrc file is listed as a project resource file.
The QML_IMPORT_PATH and QML_DESIGNER_IMPORT_PATH variables are set to the current working directory, and the QQmlApplicationEngine line adds a module import statement for the Qt.Network module to avoid a QML import error.