I'm trying to call a Python method from QML and use the return value.
QML receives undefined, from the Python method.
When passed back to Python it is just an empty string.
import sys
from PyQt5.QtCore import QObject, pyqtSlot
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
class InPython(QObject):
@pyqtSlot(str, )
def login(self, Login):
print(Login)
return "a"
if __name__ == "__main__":
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
context = engine.rootContext()
context.setContextProperty("main", engine)
engine.load('Main.qml')
win = engine.rootObjects()[0]
inPython = InPython()
context.setContextProperty("loginManger", inPython)
win.show()
sys.exit(app.exec_())
Main.qml
import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.0
ApplicationWindow {
width: 800;
height: 600;
ColumnLayout {
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
anchors.margins: 3
spacing: 3
Column {
spacing: 20
anchors.horizontalCenter: parent.horizontalCenter
TextField {
id: login
objectName: "login"
placeholderText: qsTr("Login")
focus: true
Layout.fillWidth: true
onAccepted: {
btnSubmit.clicked()
}
}
Button {
id: btnSubmit
objectName: "btnSubmit"
text: qsTr("Login")
Layout.fillWidth: true
onClicked: {
var a = loginManger.login(login.text);
console.log(a);
loginManger.login(a); // Python recieves ''
if (a === "a"){
login.text = "SUCCESS"
}
}
}
}
}
}