Get return value in QML from Python method

久未见 提交于 2019-12-20 05:37:11

问题


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)



来源:https://stackoverflow.com/questions/36206347/get-return-value-in-qml-from-python-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!