Get return value in QML from Python method

2020-04-21 08:51发布

问题:

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"
                    }
                }
            }
        }
    }
}

回答1:

You also need to tell what you are returning from your method (pay attention to the result in the pyqtslot decorator:

class InPython(QObject):
    @pyqtSlot(str, result=str)  # also works: @pyqtSlot(QVariant, result=QVariant)
    def login(self, Login):
        print(Login)
        return "a"

result – the type of the result and may be a Python type object or a string that specifies a C++ type. This may only be given as a keyword argument.

Documentation about @pyqtslot (and the result parameter)